如何将样式应用于contentcontrol的内容。例如:
<Window.Resources>
<Controls:DataGrid x:Key="PersonDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding .}" x:Shared="False">
<Controls:DataGrid.Columns>
<Controls:DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}" IsReadOnly="True"/>
<Controls:DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}" IsReadOnly="True"/>
</Controls:DataGrid.Columns>
</Controls:DataGrid>
</Window.Resources>
<StackPanel>
<ContentControl Content="{StaticResource PersonDataGrid}" DataContext="{Binding Path=Customers}" Style="DataGridStyle1"></ContentControl>
<ContentControl Content="{StaticResource PersonDataGrid}" DataContext="{Binding Path=Employees}" Style="DataGridStyle2"></ContentControl>
</StackPanel>
答案 0 :(得分:4)
编辑2:看起来你正在尝试为每个DataGrids应用不同的Style。为此,您需要在每个ContentControl的Resources部分中定义其特定的Style。如果样式在别处定义,您始终可以根据其他地方定义的样式创建新样式,如下所示
第一个ContentControl的DockPanel的背景将是黑色。第二个是蓝色。
<Window.Resources>
<Style TargetType="DockPanel" x:Key="DockStyle1">
<Setter Property="Background" Value="Black" />
</Style>
<Style TargetType="DockPanel" x:Key="DockStyle2">
<Setter Property="Background" Value="Blue" />
</Style>
<DockPanel x:Key="MyDockPanel">
<Rectangle Fill="Green" DockPanel.Dock="Top" Height="20" Width="50" />
<Rectangle Fill="Red" DockPanel.Dock="Top" Height="20" Width="20" />
<Rectangle Fill="Yellow" DockPanel.Dock="Bottom" Height="20" Width="50" />
</DockPanel>
</Window.Resources>
<StackPanel>
<StackPanel>
<ContentControl Content="{StaticResource MyDockPanel}">
<ContentControl.Resources>
<Style TargetType="{x:Type DockPanel}" BasedOn="{StaticResource DockStyle1}" />
</ContentControl.Resources>
</ContentControl>
<ContentControl Content="{StaticResource MyDockPanel}">
<ContentControl.Resources>
<Style TargetType="DockPanel" BasedOn="{StaticResource DockStyle2}" />
</ContentControl.Resources>
</ContentControl>
</StackPanel>
</StackPanel>
编辑3 - 对于你的例子,我认为你想要这样的东西(我无法测试这个,但因为我无法访问你的'Controls'命名空间):
<Window.Resources>
<Controls:DataGrid x:Key="PersonDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding .}" x:Shared="False">
<Controls:DataGrid.Columns>
<Controls:DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}" IsReadOnly="True"/>
<Controls:DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}" IsReadOnly="True"/>
</Controls:DataGrid.Columns>
</Controls:DataGrid>
</Window.Resources>
<StackPanel>
<ContentControl Content="{StaticResource PersonDataGrid}" DataContext="{Binding Path=Customers}">
<ContentControl.Resources>
<Style TargetType="{x:Type Controls:DataGrid}" BasedOn="{StaticResource DataGridStyle1}" />
</ContentControl.Resources>
</ContentControl>
<ContentControl Content="{StaticResource PersonDataGrid}" DataContext="{Binding Path=Employees}">
<ContentControl.Resources>
<Style TargetType="{x:Type Controls:DataGrid}" BasedOn="{StaticResource DataGridStyle2}" />
</ContentControl.Resources>
</ContentControl>
</StackPanel>
不幸的是,你不能像Why can't I style a DataGridTextColumn?
中所述那样设置DataGridTextColumns相反,我通常将DataGridTextColumn的CellStyle设置为'style':
<Style TargetType="DataGridCell" x:Key="DataGridCenteredText">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
<DataGridTextColumn Header="Centered Text" CellStyle="{StaticResource DataGridCenteredText}" Binding="{Binding Path=MyData}" />
我认为你需要在每列的基础上定义CellStyle(但我想不出你为什么不会这样做的原因。)