DataTrigger在DataGrid中不更新

时间:2018-07-08 14:49:36

标签: wpf datagrid datatrigger

我遇到了一个问题,并实现了一个非常小的WPF应用程序来向您展示我想要实现的目标。

摘要 :我想基于属性值将DataGrid行的背景设置为绿色。 问题 :加载后,正确的行以绿色突出显示,但是当我更新属性时,DataGrid不会更新背景色。

说明 : 我的自定义对象“用户”有一个ObservableCollection。我将其绑定到DataGrid,在其中显示“用户”的所有三个属性(字符串名字,字符串姓氏,布尔有效)。 在此DataGrid中,我有一个DataTrigger,它绑定到“活动”属性,并将行的背景设置为绿色。

在我的DataGrid下面,我有一个“激活用户”按钮,该按钮将所选用户的“活动”属性设置为“真”。

当我使用一些虚拟用户启动应用程序时,所选行的背景为绿色。但是,当我更新用户,选择另一个用户并按Button时,DataGrid不会更新(将所选行的背景更改为绿色)。

源代码

User.cs

namespace DataTriggerUpdate
{
    public class User
    {
        public bool Active { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

MainWindow.xaml

<Window x:Class="DataTriggerUpdate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:localvm="clr-namespace:DataTriggerUpdate"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="420">
    <Window.DataContext>
        <localvm:MainWindowViewModel/>
    </Window.DataContext>
    <Grid>
        <DataGrid 
                x:Name="DataGrid_UserList" 
                AutoGenerateColumns="False"  
                Width="Auto" 
                Height="100"
            BorderBrush="Black"
                ItemsSource="{Binding UserList}"
                SelectedItem="{Binding Path=SelectedUser, Mode=TwoWay}"
                IsReadOnly="True"
                Background="White"
                SelectionMode="Single" 
                SelectionUnit="FullRow"
                VerticalAlignment="Top"
                HorizontalAlignment="Center">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Active}" Value="True">
                            <Setter Property="Background" Value="LightGreen"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Client ID" Width="130" Binding="{Binding FirstName}"/>
                <DataGridTextColumn Header="User ID" Width="130" Binding="{Binding LastName}"/>
                <DataGridTextColumn Header="User ID" Width="130" Binding="{Binding Active}"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button 
            x:Name="Button_ActivateUser" 
            Content="Activate User" 
            Width="120" 
            Height="32" 
            HorizontalAlignment="Center"
            VerticalAlignment="Bottom"
            FontSize="16"
            Margin="0,20"
            Command="{Binding ActivateUserCommand}"/>
    </Grid>
</Window>

MainWindowViewModel.cs

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

namespace DataTriggerUpdate
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        #region Properties
        public ObservableCollection<User> UserList { get; set; }
        public User SelectedUser { get; set; }
        #endregion

        #region Constructor
        public MainWindowViewModel()
        {
            // Adding some dummy-users
            UserList = new ObservableCollection<User>()
            {
                new User(){FirstName = "John", LastName = "Doe", Active = false},
                new User(){FirstName = "Peter", LastName = "Miller", Active = true},
                new User(){FirstName = "Donald", LastName = "Duck", Active = false}
            };

            // Setting new RelayCommand for ActivateUser(-Button)
            ActivateUserCommand = new RelayCommand((object z) =>
            {
                try
                {
                    foreach (User user in UserList)
                    {
                        user.Active = false;
                    }
                    SelectedUser.Active = true;
                    NotifyPropertyChanged("UserList");
                }
                catch (Exception)
                {
                    return;
                }
            },
           CanExecuteSelected);
        }
        #endregion

        #region ICommand
        public ICommand ActivateUserCommand { get; set; } = null;
        #endregion

        #region CanExecute
        public bool CanExecuteSelected(object z)
        {
            if (SelectedUser != null)
                return true;
            else
                return false;
        }
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        // This method is called by the Set accessor of each property.
        // The CallerMemberName attribute that is applied to the optional propertyName
        // parameter causes the property name of the caller to be substituted as an argument.
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}

我认为我只是犯了一个小错误,但是不幸的是我自己却无法弄清楚。你能帮我吗?

0 个答案:

没有答案