【问题标题】:WPF Dependency Property not being set未设置 WPF 依赖属性
【发布时间】:2012-06-01 20:54:30
【问题描述】:

我正在尝试通过 XAML 将依赖项属性绑定到我的自定义 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 属性是一个有效属性,因为我可以在文本块中很好地显示它。我是否错误地注册了 Dependency 属性?

我需要怎么做才能在后面的代码中为AltNames 分配正确的值?

【问题讨论】:

  • 你试过AltNames="{Binding Path=AltNames, Mode=TwoWay}"吗?运行时输出窗口是否有绑定错误?
  • @nemesv 只是尝试了两种方式绑定,但没有运气。输出窗口中没有绑定错误。
  • 这很可能被 2 个共享相同名称的属性混淆了。你试过重命名一个吗?您标记为答案的解决方法似乎很奇怪!
  • 在我的例子中,该属性没有在我的UserControl 的构造函数中设置,但它在Loaded 事件中。

标签: c# .net wpf xaml


【解决方案1】:

感谢@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 ) );
    

    【讨论】:

    • 试过了,还是不行。我在 AltNames 属性的设置器上放置了一个断点,它永远不会被执行。
    • 好吧,setter 实际上从未执行过,因为绑定是在“幕后”执行的。您可以尝试this PropertyMetadata constructor 指定一个回调方法,该方法在属性更改时执行。另外,请查看How can I debug WPF bindings?
    • 这正是我刚刚所做的。使用回调设置值对我有用。感谢您让我走上正轨。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2014-05-23
    相关资源
    最近更新 更多