【问题标题】:Change control in a DataGridRow programmatically以编程方式更改 DataGridRow 中的控件
【发布时间】:2011-07-23 00:57:02
【问题描述】:

我有一个 WPF DataGrid,其中包含一个产品列表,最后一列中有一个图像按钮,用于将产品项添加到 Orders_Products 集合中。这行得通。

如果产品项目已经在 Orders_Products 集合中,我想将“添加”图像按钮更改为“删除”图像按钮。

我尝试使用 LoadingRow 事件,但似乎无法访问 Image 对象,因为在 LoadingRow 事件中还没有准备好...

我尝试了 Image 对象的 Load 事件,但如果该行在表单中不可见(当我必须滚动数据网格才能看到该行时),它不会被触发。当我对列进行排序并且该行在表单中直接可见时,它会触发。我要疯了... :(

我认为我没有做一些不寻常的事情,但我是 WPF 的新手,可能我错过了一些东西。

谁能给我一个提示? 提前致谢, 乔

P.S.:我正在使用实体框架。

【问题讨论】:

    标签: wpf wpfdatagrid


    【解决方案1】:

    执行此操作的正确方法是将DataGrid 绑定到您要显示的对象集合(您可能已经这样做了)。

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

    你可以看到上面的例子here

    DataTemplate 中,使用我对this question 的回答中描述的技术,通过匹配数据绑定Product 中适当属性的值来改变模板中显示的内容。

    所有这些都可以完全在 XAML 中完成,这是 WPF 中的首选方式。

    更新(工作示例)

    Product

    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; }
    }
    

    MainWindow.xaml:

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多