如何使用datatrigger更改WPF窗口背景?

时间:2009-10-20 14:45:59

标签: wpf xaml background datatrigger

我希望在属性更改时更改应用主窗口的背景颜色。我们有一个可以更改的营业日期,我想在预期更改时更改窗口背景。我已经设置了一个属性来告诉它。但是我可以在自行更改的窗口上设置样式数据触发器吗?或者我需要在app.xaml中执行此操作吗?

4 个答案:

答案 0 :(得分:5)

我最终做了德鲁建议的事情。除了我没有使用依赖属性。

<Window.Resources>
   <SolidColorBrush x:Key="windowBGBrush" Color="Green"/>
   <SolidColorBrush x:Key="windowBGBrushBusinessDateChanged" Color="Red"/>
</Window.Resources>
<Window.Style >
   <Style TargetType="{x:Type Window}">
      <Setter Property="Background" Value="{DynamicResource windowBGBrush}"/>
      <Style.Triggers>
         <DataTrigger Binding="{Binding IsBusinessDateChanged}" Value="true">
            <Setter Property="Background" Value="{DynamicResource windowBGBrushBusinessDateChanged}"/>
         </DataTrigger>
      </Style.Triggers>
   </Style>
</Window.Style>

IsBusinessDateChanged是我的Viewmodel上的一个属性,由服务设置。我不确定为什么这么难。

答案 1 :(得分:2)

如果您在Window上公开自定义属性,请确保将其定义为DependencyProperty,然后您应该能够使用样式中的常规触发器来响应该属性。像这样:

<Window.Style>
    <Style TargetType="{x:Type MyWindow}">
        <Style.Triggers>
            <Trigger Property="MyProperty" Value="True">
                <Setter Property="Background" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Style>

答案 2 :(得分:1)

以下是采用转换器方法的解决方案:

XAML:

<Window x:Class="StackOverflowTests.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" x:Name="window1" Width="300"
    xmlns:local="clr-namespace:StackOverflowTests">
    <Window.Resources>
        <local:DateToColorConverter x:Key="DateToColorConverter" />
    </Window.Resources>
    <Window.Background>
        <SolidColorBrush Color="{Binding ElementName=textBoxName, Path=Text, Converter={StaticResource DateToColorConverter}}" />
    </Window.Background>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBox x:Name="textBoxName" Margin="5"></TextBox>
    </Grid>
</Window>

C#:

using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace StackOverflowTests
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public class DateToColorConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime date;

            if (DateTime.TryParse(value.ToString(), out date))
            {
                if (date == DateTime.Today)
                    return Colors.Green;
                else
                    return Colors.Red;
            }
            else
            {
                return Colors.Gold;
            }
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }

        #endregion
    }

}

答案 3 :(得分:0)

也许将背景与属性绑定更好。您需要将窗口的数据源设置为对象,并且可能需要值转换器。