Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 以编程方式更改DataGridRow中的控件_Wpf_Wpfdatagrid - Fatal编程技术网

Wpf 以编程方式更改DataGridRow中的控件

Wpf 以编程方式更改DataGridRow中的控件,wpf,wpfdatagrid,Wpf,Wpfdatagrid,我有一个WPF数据网格,它包含一个产品列表,在最后一列中有一个图像按钮,用于将产品项添加到Orders_Products集合中。这很有效 现在,如果产品项已经在Orders\u Products集合中,我想将“添加”图像按钮更改为“删除”图像按钮 我试图使用LoadingRow事件,但似乎无法访问图像对象,因为LoadingRow事件中尚未准备就绪 我尝试了图像对象的加载事件,但如果该行在表单中不可见(当我必须滚动datagrid才能看到该行时),则不会触发该事件。当我对一列进行排序并且该行在表

我有一个WPF数据网格,它包含一个产品列表,在最后一列中有一个图像按钮,用于将产品项添加到Orders_Products集合中。这很有效

现在,如果产品项已经在Orders\u Products集合中,我想将“添加”图像按钮更改为“删除”图像按钮

我试图使用LoadingRow事件,但似乎无法访问图像对象,因为LoadingRow事件中尚未准备就绪

我尝试了图像对象的加载事件,但如果该行在表单中不可见(当我必须滚动datagrid才能看到该行时),则不会触发该事件。当我对一列进行排序并且该行在表单中直接可见时,它将激发。我快疯了…:(

我想我没有做什么不寻常的事情,但我是WPF的新手,可能错过了什么

有人能给我一个提示吗? 提前感谢,, 乔


注意:我正在使用实体框架。

正确的方法是将
数据网格
绑定到要显示的对象集合(您可能已经在这样做了)

然后,手动定义一个
DataGridTemplateColumn
,并将其
CellTemplate
设置为适当的
DataTemplate
(这通常被定义为
DataGrid
中的资源或元素逻辑树中更高的某个位置)

您可以看到如何完成上述操作的示例

数据模板
中,使用我的回答中描述的技术,通过匹配数据绑定
产品
中相应属性的值来改变模板中显示的内容

所有这些都可以完全在XAML中完成,这是在WPF中做事情的首选方法

更新(工作示例)
产品

public class Product
{
    public string Name { get; set; }

    public bool Exists { get; set; }
}
MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.Products = new List<Product>
            {
                new Product { Name = "Existing product", Exists = true },
                new Product { Name = "This one does not exist", Exists = false },
            };

        InitializeComponent();
        this.DataContext = this;
    }

    public IEnumerable<Product> Products { get; set; }
}
<Window x:Class="SandboxWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="ButtonColumnTemplate" >
                <ContentControl x:Name="MyContentControl" Content="{Binding}" />
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Exists}" Value="True">
                        <Setter TargetName="MyContentControl" Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <TextBlock Text="Your Remove product button goes here" />
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Exists}" Value="False">
                        <Setter TargetName="MyContentControl" Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <TextBlock Text="Your Add product button goes here" />
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Grid.Resources>
        <DataGrid ItemsSource="{Binding Products}" AutoGenerateColumns="False" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Product Name" Binding="{Binding Name}" />
                <DataGridTemplateColumn Header="Add/Remove" CellTemplate="{StaticResource ButtonColumnTemplate}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>