2012-06-01 143 views
8

我想通过XAML将依赖项属性绑定到我的自定义WPF控件。WPF依赖属性未设置

这是我如何注册依赖属性:

public static readonly DependencyProperty AltNamesProperty = 
    DependencyProperty.Register ("AltNames", typeof(string), typeof(DefectImages)); 

public string AltNames 
{ 
    get { return (string) GetValue(AltNamesProperty); } 
    set { SetValue(AltNamesProperty, value); } 
} 

,这里是我如何把它在我的XAML:

<DataGrid.Columns>     
    <DataGridTemplateColumn IsReadOnly="True"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <StackPanel Name="StackPanel1" Grid.Column="0" Width="950"> 
        <TextBlock FontSize="16" TextDecorations="None" Text="{BindingPath=StandardName}" Foreground="Black" FontWeight="Bold" Padding="5,10,0,0"></TextBlock> 
        <TextBlock Text="{Binding Path=AltNames}"TextWrapping="WrapWithOverflow" Padding="5,0,0,10"></TextBlock> 
        <!-- this part should be magic!! --> 
        <controls:DefectImages AltNames="{Binding Path=AltNames}"></controls:DefectImages> 
       </StackPanel> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid.Columns> 

我知道AltNames属性,我试图结合是一个有效的属性,因为我可以在textblock中显示它就好了。我是否注册了依赖属性?

我需要做些什么才能在我的代码中获得正确的值,分配给AltNames

+0

您是否尝试过'的altnames = “{绑定路径=的altnames,模式=双向}”'?运行时输出窗口中是否存在绑定错误? – nemesv

+0

@nemesv只是尝试了双向绑定,没有运气。输出窗口中没有绑定错误。 – jacobsimeon

+0

很可能这是由2个共享相同名称的属性混淆。你尝试重命名一个吗?您标记为答案的解决方法似乎很糟糕! – Gusdor

回答

11

感谢@Danko你为我开始。我注册了一个回调来设置属性更改时的值。
这里就是我终于结束了:

private static void OnDefectIdChanged(DependencyObject defectImageControl, DependencyPropertyChangedEventArgs eventArgs) 
{ 
    var control = (DefectImages) defectImageControl; 
    control.DefectID = (Guid)eventArgs.NewValue; 
} 

/// <summary> 
/// Registers a dependency property, enables us to bind to it via XAML 
/// </summary> 
public static readonly DependencyProperty DefectIdProperty = DependencyProperty.Register(
    "DefectID", 
    typeof (Guid), 
    typeof (DefectImages), 
    new FrameworkPropertyMetadata(
     // use an empty Guid as default value 
     Guid.Empty, 
     // tell the binding system that this property affects how the control gets rendered 
     FrameworkPropertyMetadataOptions.AffectsRender, 
     // run this callback when the property changes 
     OnDefectIdChanged 
    ) 
    ); 

/// <summary> 
/// DefectId accessor for for each instance of this control 
/// Gets and sets the underlying dependency property value 
/// </summary> 
public Guid DefectID 
{ 
    get { return (Guid) GetValue(DefectIdProperty); } 
    set { SetValue(DefectIdProperty, value); } 
} 
2

也许你需要指定PropertyMetadata参数为DependencyProperty.Register,如果属性影响如何呈现控件。例如:

DependencyProperty.Register("AltNames", typeof(string), typeof(DefectImages), 
           new FrameworkPropertyMetadata(null, 
           FrameworkPropertyMetadataOptions.AffectsRender)); 
+0

试过这个,它仍然不适合我。我在AltNames属性的setter上放置了一个断点,它永远不会被执行。 – jacobsimeon

+0

那么,setter实际上从来没有执行过,因为绑定是在“幕后”执行的。您可以尝试[此PropertyMetadata构造函数](http://msdn.microsoft.com/en-us/library/ms557327.aspx)来指定在属性更改时执行的回调方法。另外,请查看[如何调试WPF绑定?](http://bea.stollnitz.com/blog/?p=52)。 –

+0

这正是我刚刚做的。使用回调来设置为我工作的值。感谢您让我走上正轨。 – jacobsimeon