将自定义控件绑定到自己的属性
我开始对自己坚持这一点......我试图用简单的能力来定义自己的颜色。将自定义控件绑定到自己的属性
我的控制:
这是我的XAML:
<Grid>
<Grid.Resources>
<ControlTemplate x:Key="starTemplate" TargetType="{x:Type ToggleButton}">
<Viewbox>
<Path x:Name="star"
Data="F1 M 145.637,174.227L 127.619,110.39L 180.809,70.7577L 114.528,68.1664L 93.2725,5.33333L 70.3262,67.569L 4,68.3681L 56.0988,109.423L 36.3629,172.75L 91.508,135.888L 145.637,174.227 Z"
Fill="LightGray" />
</Viewbox>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="star" Property="Fill" Value="Yellow" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ToggleButton Grid.Column="0"
Click="RatingButtonClickEventHandler"
Cursor="Hand"
Tag="1"
Template="{StaticResource starTemplate}" />
<ToggleButton Grid.Column="1"
Click="RatingButtonClickEventHandler"
Cursor="Hand"
Tag="2"
Template="{StaticResource starTemplate}" />
<ToggleButton Grid.Column="2"
Click="RatingButtonClickEventHandler"
Cursor="Hand"
Tag="3"
Template="{StaticResource starTemplate}" />
<ToggleButton Grid.Column="3"
Click="RatingButtonClickEventHandler"
Cursor="Hand"
Tag="4"
Template="{StaticResource starTemplate}" />
<ToggleButton Grid.Column="4"
Click="RatingButtonClickEventHandler"
Cursor="Hand"
Tag="5"
Template="{StaticResource starTemplate}" />
</Grid>
现在,当我绑定像Fill="{Binding Path=UnselectedColor"
的Path.Fill
财产我需要指定控件的DataContext的本身:
public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.RegisterAttached("SelectedColor", typeof(Brush), typeof(RatingControl), new UIPropertyMetadata(new SolidColorBrush(Colors.Yellow)));
public RatingControl()
{
InitializeComponent();
DataContext = this;
}
这将工作,我可以定义从我的控制颜色,但它会破坏任何其他绑定。
返回从自定义控件我的实现:
例如下面的代码将设置SelectedColor
和UnselectedColor
,但因为它试图绑定到财产Rating
我RatingControl
而不是RatingValue="{Binding Path=Rating}
的结合会失败在ViewModel中的属性。
<my:RatingControl Grid.Row="0"
Grid.Column="0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
IsReadOnly="True"
RatingValue="{Binding Path=Rating,
TargetNullValue='0.0',
Mode=TwoWay}"
SelectedColor="Orange"
ToolTip="Bewertung"
UnselectedColor="LightGray" />
这就是绑定错误。 Extension
是我的ViewModel一个属性,它包含一个双财产Rating
,我试图绑定:
System.Windows.Data Error: 40 : BindingExpression path error: 'UnselectedColor' property not found on 'object' ''Extension' (HashCode=24138614)'. BindingExpression:Path=UnselectedColor; DataItem='Extension' (HashCode=24138614); target element is 'Path' (Name=''); target property is 'Fill' (type 'Brush')
你在错误的方式设置DataContext
。请阅读Simple Pattern for Creating Re-useable UserControls。
对于短期应该
<Grid x:Name="LayoutRoot">
和
public RatingControl()
{
InitializeComponent();
LayoutRoot.DataContext = this;
}
当你做到这一点
public RatingControl()
{
InitializeComponent();
DataContext = this;
}
你势必的DataContextRatingControl将其自身和每一个数据绑定的你的控制,如果没有改变dataContext后来发生,将绑定到RatingControl的属性。这就是为什么Rating绑定doens't工作(没有评级在RatingControl)。我想你应该在RatingControl的构造函数删除DataContext = this;
,并尝试结合上RatingControl填写属性SelectedColor财产RatingControl与下面的代码XAML:
Fill="{Binding ElementName=<nameOfRatingControl>, SelectedColor}"
这链接将帮助您对dataBinding有一个概述:http://www.wpftutorial.net/DataBindingOverview.html。 希望得到这个帮助。
对不起,我无法做任何与您的答案来帮助我解决我的问题。 – SeToY
@SeToY当您需要在其模板或样式中引用控件的属性时,删除'DataContext = this'行,使用'RelativeSource = {RelativeSource AncestorType = my:RatingControl}进行绑定。 – Athari