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

标签 c# wpf binding custom-controls

我对此快要发疯了...我试图为我的控件提供定义自己的颜色的简单功能。

我的控制:

这是我的 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>

现在,当我绑定(bind) Path.Fill 属性(如 Fill="{Binding Path=UnselectedColor")时,我需要将控件的 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 中的一个属性,它包含一个我试图绑定(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/

相关文章:

c# - C# 对 Java Web 服务的请求中 header 和请求之间的奇怪字符会阻止成功

c# - 如何从另一个脚本访问任何创建的游戏对象的属性 Unity C# 2D

c# - WPF 开源项目

wpf - ItemsSource 绑定(bind)且不起作用 PropertyChanged

javascript - IE9 和 10 不反射(reflect) javascript 光标更改

c# - .Net 正则表达式 : what is the word character\w?

c# - 使用数据集进行 Microsoft 报告

c# - 如何获取触发器以根据 DataContext 属性更改 TextBlock 的颜色?

jquery解绑有必要吗? (替换元素)

silverlight - MVVM View 绑定(bind)