开发者

Change control in a DataGridRow programmatically

I have a WPF DataGrid contains a list of Products with an image button in the last column to add the P开发者_如何转开发roduct item into the Orders_Products collection. That works.

Now I'd like to change the "add" image button with a "delete" image button if the Product item is already in the Orders_Products collection.

I tried to use the LoadingRow event but it seems there's no way to access the Image object because is not ready yet in the LoadingRow event...

I tried the Load event of the Image object, but it's not fired if the row is not visible in the form (when I must scroll the datagrid to see that row). It fires when I sort a column and the row is directly visible in the form. I'm going crazy... :(

I think I'm not doing something unusual but I'm new to WPF and probably I miss something.

Can someone give me an hint? Thanks in advance, Joe

P.S.: I'm using Entity Framework.


The proper way of doing this is by binding the DataGrid to the collection of objects you want to display (you are probably doing this already).

Then, manually define a DataGridTemplateColumn and set its CellTemplate to an appropriate DataTemplate (this would usually be defined as a resource in your DataGrid or somewhere higher in the logical tree of elements).

You can see an example of how the above is done here.

Inside the DataTemplate, use the technique described in my answer to this question to vary what is displayed in the template by matching the value of an appropriate property in the databound Product.

All of this can be done entirely in XAML, as is the preferred way of doing things in WPF.

Update (working example)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜