2012-07-03 64 views
2

我做了我自己的控件。一個從DataGrid繼承而另一個從ContentControl繼承。其中一個獲取另一個,所以我嘗試公開他們的屬性,但由於我需要許多不同的控件,我想爲我的控件創建一個樣式(從DataGrid繼承的樣式),並將屬性從此控件設置爲我的ContentControl。我只是寫了這樣的代碼,但它不起作用。任何人都知道我做錯了什麼?造型自定義控件

<Style x:Key="CustomDataGridStyle" 
     TargetType="{x:Type controls:CustomDataGrid}"> 
    <Setter Property="CurrentRow" 
      Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=SelectedItem, Mode=TwoWay}" /> 
    <Setter Property="CaptionVisibility" 
      Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=CaptionVisibility, Mode=TwoWay}" /> 
    <Setter Property="CaptionText" 
      Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=CaptionText, Mode=TwoWay}" /> 
    <Setter Property="RowValidationErrorTemplate" 
      Value="{StaticResource BasicRowValidationErrorTemplate}" /> 
    <Setter Property="CurrentView" 
      Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=CurrentView, Mode=OneWayToSource}" /> 
    <Setter Property="CurrentColumnHeaderText" 
      Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=CurrentColumnHeader, Mode=OneWayToSource}" /> 
    <Setter Property="SelectedCellText" 
      Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=SelectedText, Mode=OneWayToSource}" /> 
    <Setter Property="IsDataGridFocused" 
      Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=HasFocus, Mode=OneWayToSource}" /> 
</Style> 

而且我已經定義我的控制這樣

<controls:CustomDataGrid x:Key="DataGridOne" AutoGenerateColumns="True" x:Shared="False" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}" /> 

和另一個

<controls:DataGridContainer Content="{StaticResource DataGridOne}" DataContext="{Binding Products}" 
              x:Name="dataGridOne" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, 
             AncestorType={x:Type UserControl}}, 
             Path=DataContext.SelectedItem, Mode=TwoWay}" CaptionVisibility="Collapsed"/> 

回答

0

你的風格有X:主要屬性設置。這意味着它不會適用於默認情況下所有類型的控件。您應該刪除Key屬性以使樣式默認並應用於所有CustomDataGrid控件,或者像CustomDataGrid定義中的引用樣式,如下所示:

<Window> 
    <Window.Resources> 
    <Style x:Key="CustomDataGridStyle" TargetType="{x:Type controls:CustomDataGrid}"> 
    ... 
    </Style> 
    </Window.Resources> 

<controls:CustomDataGrid ... Style="{StaticResource CustomDataGridStyle}" ... /> 
</Window> 
+0

我已經試過了。而且我已將CustomDataGrid定義爲資源,您認爲這是我的問題嗎?我不能將一個樣式分配給我的資源內的其他控件? – Nandhi

+0

創建一個沒有綁定但只有一個簡單值的setter。檢查這是否工作。問題可能與您認爲的不同。 –

+0

我嘗試使用類似這樣的''''and works ,但我需要的是將屬性從CustomDataGrid公開到DataGridContainer – Nandhi