WPF 数据触发器不起作用. [英] WPF DataTrigger doesn't work.

查看:36
本文介绍了WPF 数据触发器不起作用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个 WPF 页面,应该可以更改主题(深色主题和浅色主题).我是 WPF 的新手,使用 DataTrigger 找到了解决我的问题的方法,但它不起作用.3 小时后,我尝试了 10 种不同的解决方案/教程,但我不知道我做错了什么......

I designed a WPF Page and it should be possible to change the theme (dark theme and light theme). I am a newbie in WPF and found a solution to my problem using DataTrigger, but it don't works. 3 hours later I tried like 10 different solutions/tutorials but I don't know what I am doing wrong...

xml 代码:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:VMQWPFApplication.Pages" x:Class="VMQWPFApplication.Pages.MainPage" 
  mc:Ignorable="d" 
  d:DesignHeight="400" d:DesignWidth="600"
Title="MainPage">

<Page.Resources>
    <Style x:Key="styleWithTrigger" TargetType="{x:Type Rectangle}">
        <Setter Property="Fill" Value="Blue"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding DarkTheme, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainPage}}}" Value="True">
                <Setter Property="Fill" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Page.Resources>

<DockPanel>
    <!--Toolbar-->
    ...

    <!--Body-->
    <Grid>
        <Rectangle Style="{StaticResource styleWithTrigger}"/>
    </Grid>
</DockPanel>

这里是 cs:

namespace VMQWPFApplication.Pages
{
    /// <summary>
    /// Interaction logic for MainPage.xaml
    /// </summary>
    public partial class MainPage : Page
    {
        public bool DarkTheme { get; set; }

        public MainPage()
        {
            InitializeComponent();
            DarkTheme = false;
        }

        private void TestButton_Click(object sender, RoutedEventArgs e)
        {
            DarkTheme = true;
        }
    }
}

一开始矩形是蓝色的,但它不会改变.

At the beginning the rectangle is blue, but it won't change.

推荐答案

您的 MainPage.xaml.cs 文件未实现 INotifyPropertyChanged 接口.为此,您应该添加/更改以下内容:

Your MainPage.xaml.cs file does not implement the INotifyPropertyChanged interface. To do so you should add/change the following:

public partial class MainPage : Page, INotifyPropertyChanged

#region INotifyPorpertyChanged Memebers 

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

#endregion

我会将您的 DarkTheme 属性更改为:

I would change your DarkTheme property to:

private bool _darkTheme;
public bool DarkTheme { get { return _darkTheme; } set { _darkTheme = value; NotifyPropertyChanged("DarkTheme"); }

现在,当您更新 DarkTheme 时,它​​将引发更改属性事件.我也会把 DataContext 放入 Page 组成:

Now when you update DarkTheme it will raise the Change Property Event. I would also put the DataContext into the Page make up:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

这篇关于WPF 数据触发器不起作用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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