gpt4 book ai didi

c# - 将 CustomControl 绑定(bind)到自己的属性

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:10 25 4
gpt4 key购买 nike

我快把自己逼疯了...我正在尝试为我的控件提供定义自己的颜色的简单功能。

我的控制:

那是我的 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" 那样绑定(bind) 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;
}

这会起作用,我可以从我的控件中定义颜色,但它会破坏任何其他绑定(bind)。

从自定义控件回到我的实现:

例如,下面的代码将设置 SelectedColorUnselectedColor,但是 RatingValue="{Binding Path=Rating} 的绑定(bind)将失败,因为它试图绑定(bind)到我的 RatingControl 中的属性 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" />

那是绑定(bind)错误。 Extension 是我的 ViewModel 中的一个属性,它包含一个 double 我试图绑定(bind)的属性 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;
}

关于c# - 将 CustomControl 绑定(bind)到自己的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17392320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com