I am trying to bind a dependency property via XAML to my custom WPF control.
我试图通过XAML将依赖项属性绑定到我的自定义WPF控件。
Here's how I register the dependency property:
这是我如何注册依赖属性:
public static readonly DependencyProperty AltNamesProperty =
DependencyProperty.Register ("AltNames", typeof(string), typeof(DefectImages));
public string AltNames
{
get { return (string) GetValue(AltNamesProperty); }
set { SetValue(AltNamesProperty, value); }
}
And Here's how I call it in my XAML:
以下是我在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>
I know the AltNames
property that I'm attempting to bind to is a valid property because I can display it in a textblock just fine. Am I registering the Dependency property incorrectly?
我知道我试图绑定的AltNames属性是一个有效的属性,因为我可以在文本块中显示它。我是否错误地注册了Dependency属性?
What do I need to do to get the correct value assigned to AltNames
in my code behind?
我需要做什么才能在我的代码中获得分配给AltNames的正确值?
2 个解决方案
#1
11
Thanks to @Danko for getting me started. I registered a callback to set the value when the property changes.
Here's what I finally ended up with:
感谢@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
2
Maybe you need to specify the PropertyMetadata
argument to DependencyProperty.Register
, if the property affects how the control is rendered. For example:
如果属性影响控件的呈现方式,可能需要为DependencyProperty.Register指定PropertyMetadata参数。例如:
DependencyProperty.Register("AltNames", typeof(string), typeof(DefectImages),
new FrameworkPropertyMetadata( null,
FrameworkPropertyMetadataOptions.AffectsRender ) );
#1
11
Thanks to @Danko for getting me started. I registered a callback to set the value when the property changes.
Here's what I finally ended up with:
感谢@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
2
Maybe you need to specify the PropertyMetadata
argument to DependencyProperty.Register
, if the property affects how the control is rendered. For example:
如果属性影响控件的呈现方式,可能需要为DependencyProperty.Register指定PropertyMetadata参数。例如:
DependencyProperty.Register("AltNames", typeof(string), typeof(DefectImages),
new FrameworkPropertyMetadata( null,
FrameworkPropertyMetadataOptions.AffectsRender ) );