基于视图(WPF)更改窗口的样式

时间:2015-10-08 12:39:48

标签: c# wpf xaml

我有多个窗口和一个共同的风格。我需要根据窗口更改边框背景颜色。如果是窗口' A'如果显示那么背景应该是黑色的,如果' B'然后是白色等等。我正在使用自定义的basewindowview。我可以使用datatrigger吗?

Code Style.xaml

<security-constraint>
    <web-resource-collection>
        <url-pattern>/bg/c/portal/protected</url-pattern>
        <http-method>POST</http-method>
        <http-method>GET</http-method>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>
<security-constraint>
    <web-resource-collection>
        <url-pattern>/bg/c/portal/protected</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>

这是我的观点

<Style x:Key="CustomWindowStyle"
   TargetType="{x:Type Window}">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Padding" Value="{StaticResource WindowPadding}"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Window}">
            <Grid Visibility="{TemplateBinding Visibility}">
                <Grid.Resources>
                    <Style TargetType="{x:Type Rectangle}">
                        <Setter Property="Fill" Value="Transparent"/>
                    </Style>
                </Grid.Resources>
                <Grid.RowDefinitions>
                    <RowDefinition Height="5"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="5"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="5"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="5"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.ColumnSpan="3"
                        Grid.RowSpan="3">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Border x:Name="PART_TitleBar"
                            Style="{StaticResource WindowTitleBarBorderStyle}"
                            Padding="0">                                    
                        </Border>
                    <Border Grid.Row="1" Style="{StaticResource WindowBorderStyle}">
                        <ContentControl Grid.Row="1"
                                        Content="{TemplateBinding Content}"
                                        Background="{TemplateBinding Background}"
                                        Margin="{TemplateBinding Padding}"/>
                </Border>
            </Grid>
            <ControlTemplate.Triggers>              
                <DataTrigger Binding="{Binding ElementName="MessageBox",RelativeSource={RelativeSource Self}, Mode=OneWay}" Value="True">
                    <Setter TargetName="PART_TitleBar" Property="Background" Value="White" />
                </DataTrigger>                        
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

这里我使用datatrigger来更改值,elementname是视图的名称。

0 个答案:

没有答案