2013-06-30 67 views
2

我開始對自己堅持這一點......我試圖用簡單的能力來定義自己的顏色。將自定義控件綁定到自己的屬性

我的控制:

這是我的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; 
    } 

這將工作,我可以定義從我的控制顏色,但它會破壞任何其他綁定。

返回從自定義控件我的實現:

例如下面的代碼將設置SelectedColorUnselectedColor,但因爲它試圖綁定到財產RatingRatingControl而不是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')

回答

0

當你做到這一點

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。 希望得到這個幫助。

相關問題