wpf - 从样式中将项目添加到集合
问题描述
我有一种以网格内的 DataGrids 为目标的样式。
<Grid.Resources>
<Style TargetType="DataGrid">
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="AutoGenerateColumns" Value="False" />
</Style>
</Grid.Resources>
我想在样式中添加一个 EventTrigger(使用WPF 的 Microsoft XAML 行为库)。
<b:EventTrigger EventName="SelectionChanged">
<b:InvokeCommandAction Command="{Binding UpdateSelection}" CommandParameter="{Binding SelectedItem}" />
</b:EventTrigger>
然而,这:
<Grid.Resources>
<Style TargetType="DataGrid">
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="AutoGenerateColumns" Value="False" />
<Setter Property="b:Interaction.Triggers">
<Setter.Value>
<b:EventTrigger EventName="SelectionChanged">
<b:InvokeCommandAction Command="{Binding UpdateSelection}" CommandParameter="{Binding SelectedItem}" />
</b:EventTrigger>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
失败,出现XamlParseException:
'设置属性'System.Windows.Setter.Property' 引发异常。行号“28”和行位置“22”。
内部异常 1:
ArgumentNullException:值不能为空。
参数名称:属性
尽管报告的异常似乎无关紧要,但我认为这是因为我试图Interaction.Triggers
使用单个 设置附加的集合属性EventTrigger
,而不是添加EventTrigger
到集合中。
如何在 XAML 中的样式中将项目添加到集合中?
解决方案
您不能在Style
. 不支持。
您可以做的是定义一个连接事件处理程序的附加行为:
public class SelectionChangedBehavior
{
public static ICommand GetCommand(DataGrid dataGrid)
{
return (ICommand)dataGrid.GetValue(CommandProperty);
}
public static void SetCommand(DataGrid dataGrid, ICommand value)
{
dataGrid.SetValue(CommandProperty, value);
}
public static object GetCommandParameter(DataGrid dataGrid)
{
return dataGrid.GetValue(CommandParameterProperty);
}
public static void SetCommandParameter(DataGrid dataGrid, object value)
{
dataGrid.SetValue(CommandParameterProperty, value);
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached(
"Command",
typeof(ICommand),
typeof(SelectionChangedBehavior),
new UIPropertyMetadata(null, OnCommandChanged));
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.RegisterAttached(
"CommandParameter",
typeof(object),
typeof(SelectionChangedBehavior),
new UIPropertyMetadata(null));
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DataGrid dataGrid = d as DataGrid;
if (dataGrid != null)
dataGrid.SelectionChanged += OnSelectionChanged;
}
private static void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dataGrid = (DataGrid)sender;
ICommand command = GetCommand(dataGrid);
if (command != null)
command.Execute(GetCommandParameter(dataGrid));
}
}
XAML:
<Style TargetType="DataGrid" xmlns:local="clr-namespace:WpfApp1">
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="AutoGenerateColumns" Value="False" />
<Setter Property="local:SelectionChangedBehavior.Command" Value="{Binding UpdateSelection}" />
<Setter Property="local:SelectionChangedBehavior.CommandParameter" Value="{Binding SelectedItem}" />
</Style>
WpfApp1
指上SelectionChangedBehavior
例中类的命名空间。
推荐阅读
- reactjs - 我找不到如何使用 useEffect 钩子和 axios 发出 HTTP POST 请求的示例
- javascript - 如何在本机反应中将参数发送到父组件
- microprocessors - 为什么 MOV M,M 无效,而 MOV A,A 在 8085 微处理器中有效
- spring - 带有 Spring Boot 和 localstack 的 AWS Secrets Manager
- android - 使用 Firebase 进行 Android 注册,但添加信息未保存到实时数据库
- visual-studio - 如何正确地将对象发送到函数?
- c# - 如何检查类中的对象是否不为空
- mongodb - 有没有办法确定 oplog 切片在用于 mongo 备份的 pbm 列表中属于哪个特定快照?
- javascript - 如何从 HERE 地图中的组中获取特定对象
- python - 使用递归的最小成本路径