将CustomControl绑定到自己的属性

时间:2013-06-30 16:12:47

标签: 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>

现在当我绑定像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中的属性RatingControl 1}}而不是我的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路径错误:'object'''Extension'(HashCode = 24138614)'上找不到'UnselectedColor'属性。 BindingExpression:路径= UnselectedColor; DataItem ='Extension'(HashCode = 24138614); target元素是'Path'(Name =''); target属性为'Fill'(类型'Brush')

3 个答案:

答案 0 :(得分:1)

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

简而言之,它应该是

<Grid x:Name="LayoutRoot">

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

答案 1 :(得分:0)

当您需要绑定到self时,可以使用Binding.RelativeSource Property

另见:

答案 2 :(得分:0)

当你这样做时

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

RatingControl DataContext 绑定到自身以及控件上的每个数据绑定,如果以后没有更改dataContext,将绑定到 RatingControl 的属性。这就是为什么评级绑定无法正常工作(评级控件中没有评分)。我认为您应该删除 RatingControl 的构造函数中的DataContext = this;,并尝试将填充属性绑定到 SelectedColor 属性 RatingControl 以及 RatingControl XAML 上的以下代码:

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

此链接可帮助您全面了解dataBinding:http://www.wpftutorial.net/DataBindingOverview.html。 希望这有帮助。