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"/>
我已經試過了。而且我已將CustomDataGrid定義爲資源,您認爲這是我的問題嗎?我不能將一個樣式分配給我的資源內的其他控件? – Nandhi
創建一個沒有綁定但只有一個簡單值的setter。檢查這是否工作。問題可能與您認爲的不同。 –
我嘗試使用類似這樣的''''and works ,但我需要的是將屬性從CustomDataGrid公開到DataGridContainer – Nandhi