gpt4 book ai didi

wpf - 当行中项目的属性更改时,如何更改 WPF DataGrid 控件中该行的样式

转载 作者:行者123 更新时间:2023-12-04 23:26:30 25 4
gpt4 key购买 nike

我有一个 DataGrid我的 WPF 应用程序中包含一个对象的控件。该对象有一个 bool 属性,可以通过用户操作进行更改。当该属性的值更改时,我需要更改行的样式。

我写了一个继承自 StyleSelector 的类:

public class LiveModeSelector : StyleSelector {

public Style LiveModeStyle { get; set; }
public Style NormalStyle { get; set; }

public override Style SelectStyle( object item, DependencyObject container ) {
DataGridRow gridRow = container as DataGridRow;
LPRCamera camera = item as LPRCamera;
if ( camera != null && camera.IsInLiveMode ) {
return LiveModeStyle;
}
return NormalStyle;
}
}

有问题的 View 模型类实现了 INotifyPropertyChanged ,它引发了 PropertyChanged当相关属性发生变化时发生的事件。
// Note:  The ModuleMonitor class implements INotifyPropertyChanged and raises the PropertyChanged
// event in the SetAndNotify generic method.
public class LPRCamera : ModuleMonitor, ICloneable {

. . .

public bool IsInLiveMode {
get { return iIsInLiveMode; }
private set { SetAndNotify( "IsInLiveMode", ref iIsInLiveMode, value ); }
}
private bool iIsInLiveMode;

. . .

/// </summary>
public void StartLiveMode() {
IsInLiveMode = true;

. . .
}


public void StopLiveMode() {
IsInLiveMode = false;

. . .
}
}

当用户执行所需的操作时,属性的值会更改,但样式不会更改。

我在 SelectStyle 方法中放置了一个断点,当控件首次加载时我看到断点被击中,但是当属性的值更改时它没有被击中。

我错过了什么?

最佳答案

我不认为 StyleSelector倾听 PropertyChange通知,所以当 IsInLiveMode 时它不会重新运行属性变了。

将您的风格放入 DataTrigger基于 IsInLiveMode取而代之的是,只要属性更改通知被提出,它就会被重新评估。

<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}" x:Key="Style1">
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="{x:Type DataGridRow}" x:Key="Style2">
<Setter Property="Background" Value="Blue" />
</Style>
</DataGrid.Resources>

<DataGrid.Style>
<Style TargetType="{x:Type DataGrid}">
<Setter Property="RowStyle" Value="{StaticResource Style1}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyDataGrid, Path=DataContext.IsInLiveMode}" Value="True">
<Setter Property="RowStyle" Value="{StaticResource Style2}" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Style>

关于wpf - 当行中项目的属性更改时,如何更改 WPF DataGrid 控件中该行的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12147552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com