【问题标题】:Binding CustomControl to own properties将 CustomControl 绑定到自己的属性
【发布时间】:2013-06-30 16:12:47
【问题描述】:

我在这方面让自己发疯......我试图为我的控件提供定义自己颜色的简单能力。

我的控制:

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

现在,当我将 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;
    }

这可行,我可以从我的控件中定义颜色,但它会破坏任何其他绑定。

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

例如,以下代码将设置SelectedColorUnselectedColor,但RatingValue="{Binding Path=Rating} 的绑定将失败,因为它尝试绑定到我的Rating 中的属性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 中的一个属性,它包含我正在尝试绑定的 double 属性 Rating

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“扩展”(HashCode=24138614)上找不到“UnselectedColor”属性。 BindingExpression:Path=UnselectedColor; DataItem='扩展名' (HashCode=24138614);目标元素是'路径'(名称='');目标属性是“填充”(输入“画笔”)

【问题讨论】:

    标签: c# wpf binding custom-controls


    【解决方案1】:

    您以错误的方式设置DataContext。请阅读Simple Pattern for Creating Re-useable UserControls

    简而言之应该是

    <Grid x:Name="LayoutRoot">
    

    public RatingControl()
    {
        InitializeComponent();
        LayoutRoot.DataContext = this;
    }
    

    【讨论】:

      【解决方案2】:

      当你这样做时

      public RatingControl()
      {
          InitializeComponent();
          DataContext = this;
      }
      

      您将 RatingControlDataContext 绑定到自身,并且您的控件上的每个数据绑定,如果以后没有更改 dataContext,将绑定到 RatingControl的属性。这就是 Rating 绑定不起作用的原因(RatingControl 中没有 Rating)。我认为您应该在 RatingControl 的构造函数中删除 DataContext = this;,并尝试将 Fill 属性绑定到 RatingControlSelectedColor 属性/strong> 在 RatingControl XAML 上有以下代码:

      Fill="{Binding ElementName=<nameOfRatingControl>, SelectedColor}"
      

      此链接将帮助您了解有关 dataBinding 的概述:http://www.wpftutorial.net/DataBindingOverview.html。 希望对您有所帮助。

      【讨论】:

        【解决方案3】:

        当需要绑定自身时,可以使用Binding.RelativeSource Property

        另见:

        【讨论】:

        • 抱歉,您的回答无法帮助我解决问题。
        • @SeToY 删除DataContext = this 行,当您需要在其模板或样式中引用控件的属性时使用RelativeSource={RelativeSource AncestorType=my:RatingControl} 绑定。
        猜你喜欢
        • 1970-01-01
        • 2015-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多