[英]Access auto generated buttons inside WPF Datagrid by their xNames
I have this DataGrid filled with products and there stocks and as well as an auto generated button per items, i want to call this auto generated button using there xNames so that when the stocks of a specific item became 0 i will simply disable it's button which is also auto generated, but my problem is i cannot call it.我有这个 DataGrid 充满了产品和库存以及每个项目的自动生成按钮,我想使用那里的 xNames 调用这个自动生成的按钮,这样当特定项目的库存变为 0 时,我将简单地禁用它的按钮也是自动生成的,但我的问题是我不能调用它。
I tried a lot such using FindNames in datagrid but still won't work\我在数据网格中使用 FindNames 尝试了很多,但仍然无法正常工作\
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding id}" Header="Product ID" Width="Auto"></DataGridTextColumn>
<DataGridTextColumn ElementStyle="{StaticResource ProductNameCellMargin}" IsReadOnly="True" Binding="{Binding item_name}" Header="Product Name" Width="*"></DataGridTextColumn>
<DataGridTextColumn ElementStyle="{StaticResource ProductNameCellMargin}" IsReadOnly="True" Binding="{Binding item_price}" Header="Price" Width="*"></DataGridTextColumn>
<DataGridTextColumn ElementStyle="{StaticResource ProductNameCellMargin}" IsReadOnly="True" Binding="{Binding item_quantity}" Header="Quantity" Width="*"></DataGridTextColumn>
<DataGridTemplateColumn IsReadOnly="True" HeaderStyle="{StaticResource CenterGridHeaderStyle}" Header="Remove" MinWidth="50" CanUserSort="False" CanUserResize="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Background="Transparent" BorderBrush="Transparent" x:Name="select_item_btn" Padding="5, 2, 5, 2" Cursor="Hand" Click="remove_btn_Click">
<DockPanel>
<Image Height="18" Width="19" Source="img/Remove.png" />
</DockPanel>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
You could use a DataTrigger
to disable the Button
based on the value of some source property, eg:您可以使用
DataTrigger
根据某些源属性的值禁用Button
,例如:
<Button Background="Transparent" BorderBrush="Transparent" x:Name="select_item_btn"
Padding="5, 2, 5, 2" Cursor="Hand" Click="remove_btn_Click">
<DockPanel>
<Image Height="18" Width="19" Source="img/Remove.png" />
</DockPanel>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding item_quantity}" Value="0">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
The above sample markup should disable the Button
when the item_quantity
property is 0
.当
item_quantity
属性为0
时,上面的示例标记应该禁用Button
。
Rather than handling click events it is usual in wpf to use a command with a button. wpf 通常不处理点击事件,而是使用带有按钮的命令。 Bind the command property of the button to an icommand in the parent datacontext / viewmodel or the item viewmodel.
将按钮的命令属性绑定到父数据上下文/视图模型或项目视图模型中的 icommand。
ICommand has CanExecute. ICommand 有 CanExecute。
https://learn.microsoft.com/en-us/do.net/api/system.windows.input.icommand.canexecute?view.net-7.0#system-windows-input-icommand-canexecute(system-object) https://learn.microsoft.com/en-us/do.net/api/system.windows.input.icommand.canexecute?view.net-7.0#system-windows-input-icommand-canexecute(系统对象)
If that returns false then the button will be disabled and not run.如果返回 false,那么该按钮将被禁用并且不会运行。
https://learn.microsoft.com/en-us/answers/questions/433293/mvvm-relaycommand-icommand https://learn.microsoft.com/en-us/answers/questions/433293/mvvm-relaycommand-icommand
The community toolkit mvvm makes defining commands pretty easy.社区工具包 mvvm 使定义命令变得非常容易。
https://blogs.msmvps.com/bsonnino/2022/08/06/the-mvvm-pattern-revisited-with-the-mvvm-community-toolkit-8-0/ https://blogs.msmvps.com/bsonnino/2022/08/06/the-mvvm-pattern-revisited-with-the-mvvm-community-toolkit-8-0/
Here the last lambda expression is canexecute:这里最后一个 lambda 表达式是可以执行的:
RemoveCommand = new RelayCommand(DoRemove, () => SelectedCustomer != null);
You would be checking stocks somehow instead of selectedcustomer, more like:你会以某种方式检查股票而不是 selectedcustomer,更像是:
RemoveCommand = new RelayCommand(DoRemove, () => StockLevel > 0);
But, as the article illustrates, you can instead use an attribute但是,如文章所示,您可以改用属性
[RelayCommand(CanExecute = "HasStock")]
private void Remove()
I'm not sure what remove button does but you might want a command parameter for the row and the command in the parent viewmodel if you're removing a given row out the datagrid.我不确定删除按钮的作用,但如果要从数据网格中删除给定的行,您可能需要该行的命令参数和父视图模型中的命令。
A RelayCommand is designed to receive a parameter to it's method. RelayCommand 旨在接收其方法的参数。
A relativesource binding would allow you to bind from the button to the DataContext.RemoveCommand if necessary.如果需要,相对源绑定将允许您从按钮绑定到 DataContext.RemoveCommand。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.