WPF ContentControl样式 [英] WPF ContentControl Styling

查看:90
本文介绍了WPF ContentControl样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将样式应用于contentcontrol的内容。例如:

How can I apply a style to the content of a contentcontrol. For example:

<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>


推荐答案

编辑2:看来您正在尝试将不同的样式应用于每个DataGrid。为此,您需要在每个ContentControl的资源部分中定义其特定样式。如果样式是在其他地方定义的,则始终可以根据其他地方定义的样式创建新样式,如下所示。

EDIT 2: It looks like you're trying to apply a different Style to each of your DataGrids. To do this, you're going to need to define their specific Style inside the Resources Section of each ContentControl. If the styles are defined elsewhere, you can always create a new style based on the style defined elsewhere as shown below

第一个ContentControl的DockPanel的背景将是黑色。第二个是蓝色。

The first ContentControl's DockPanel's Background will be Black. The second's will be Blue.

<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-以您的示例为例,我认为您想要这样的东西(我无法对此进行测试但是,因为我无权访问您的控件命名空间):

EDIT 3 - For your example, I think you want something like this (I can't test this however as I don't have access to your 'Controls' namespace):

<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>

不幸的是,您不能按照为什么不能设置DataGridTextColumn的样式?

Unfortunately, you cannot Style DataGridTextColumns as stated in Why can't I style a DataGridTextColumn?

将DataGridTextColumn的CellStyle设置为样式:

Instead, I generally set the CellStyle of a DataGridTextColumn to 'style' it:

<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(我不能想起为什么您仍然不会的任何原因。)

I think you'll need to define the CellStyle on a per column basis however (I can't think of any reason why you wouldn't anyway.)

这篇关于WPF ContentControl样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆