绑定WPF DataGrid后如何修改单元格

本文关键字:修改 单元格 何修改 WPF DataGrid 绑定 | 更新日期: 2023-09-27 18:15:55

我正在尝试更新WPF数据网格中单元格的外观,我来自ASP。. NET Webforms,但我找不到一个数据绑定或类似的东西,我可以进入进行更改。这在WPF中是如何工作的?

我只有这个:

 private void BindItems(Cars model)
 {
      ObservableCollection<CarItem> items = model.GetCarItems();
      CarsGrid.DataContext = items;
       // I tried updating below this line but the rows are not there yet (obviously), 
       // I was hoping there was like a CarsGrid.DataBind(); or something.
  }

这是复选框的标记,基本上我想启用购买,如果绑定值为真,如果它为真,则禁用租赁,反之亦然,如果租金为真,则禁用购买

<DataGridCheckBoxColumn Header="Add" Width="75" Binding="{Binding Buy}">
                    <DataGridCheckBoxColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                        </Style>
                    </DataGridCheckBoxColumn.HeaderStyle>
                </DataGridCheckBoxColumn>
                <DataGridCheckBoxColumn Header="Remove" Width="75"  Binding="{Binding Rent}">
                    <DataGridCheckBoxColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                        </Style>
                    </DataGridCheckBoxColumn.HeaderStyle>
                </DataGridCheckBoxColumn>

绑定WPF DataGrid后如何修改单元格

在Datagrid中启用和禁用内容的方法与WPF中的其他项相同,只需在您想要的任何项上设置IsEnabled属性。在您的情况下,您需要设置要使用的单元格样式,以便覆盖用于显示复选框*和按钮的模板(在我的示例中)。

*:通常在DataGridCheckBoxColumn中使用的复选框模板需要一次点击来选择行,一次点击来切换值,而使用下面的方法,一次点击就可以了。

请参阅下面的示例,其中有两个复选框启用和禁用两个按钮。显然,这两个复选框不需要是可见的;它们与你的数据有关。如果您需要在bool值之间的复杂逻辑,那么您可以在代码中完成,或者使用Converter根据上下文设置每个项的可见性。

视图:

<Grid.Resources>
    <Style TargetType="{x:Type DataGridCell}" x:Key="BuyTemplate">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <CheckBox HorizontalAlignment="Center" IsChecked="{Binding Buy, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type DataGridCell}" x:Key="RentTemplate">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <CheckBox HorizontalAlignment="Center" IsChecked="{Binding Rent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <DataTemplate x:Key="BuyButtonTemplate">
        <Button HorizontalAlignment="Center" IsEnabled="{Binding Buy}" Content="BUY NOW!" />
    </DataTemplate>
    <DataTemplate x:Key="RentButtonTemplate">
        <Button HorizontalAlignment="Center" IsEnabled="{Binding Rent}" Content="Rent this car!" />
    </DataTemplate>
</Grid.Resources>
<DataGrid Name="CarGrid" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
        <DataGridCheckBoxColumn Header="IsBuy" Width="75" CellStyle="{StaticResource ResourceKey=BuyTemplate}" />
        <DataGridCheckBoxColumn Header="IsRent" Width="75"  CellStyle="{StaticResource ResourceKey=RentTemplate}" />
        <DataGridTemplateColumn Header="Buy" CellTemplate="{StaticResource ResourceKey=BuyButtonTemplate}" />
        <DataGridTemplateColumn Header="Rent" CellTemplate="{StaticResource ResourceKey=RentButtonTemplate}" />
    </DataGrid.Columns>
</DataGrid>

后台代码:

class Car : INotifyPropertyChanged
{
    private bool buy, rent;
    public bool Buy 
    {
        get { return buy; }
        set { SetAndNotifyIfChanged(ref buy, value); }
    }
    public bool Rent 
    {
        get { return rent; }
        set { SetAndNotifyIfChanged(ref rent, value); }
    }
    public string Name { get; set; }
    public Car(string name, bool isBuy)
    {
        Name = name;
        buy = isBuy;
        rent = !isBuy;
    }
    private void SetAndNotifyIfChanged<T>(ref T original, T newValue, [CallerMemberName] string caller = null)
        where T : IEquatable<T>
    {
        if (!original.Equals(newValue))
        {
            original = newValue;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(caller));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

我这样设置条目:

List<Car> cars = new List<Car>();
cars.Add(new Car("Citroen", true));
cars.Add(new Car("Ford", true));
cars.Add(new Car("Toyota", false));
CarGrid.ItemsSource = cars;

请参见样式。触发属性。你可以这样做(参见用星号标记的文本):

<DataGridCheckBoxColumn Header="Add" Width="75" Binding="{Binding Buy}">
    **<DataGridCheckBoxColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=YourPropertyName}" Value="False">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridCheckBoxColumn.CellStyle>**   
    <DataGridCheckBoxColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center" />
                    </Style>
                </DataGridCheckBoxColumn.HeaderStyle>
    </DataGridCheckBoxColumn>
    <DataGridCheckBoxColumn Header="Remove" Width="75"  Binding="{Binding Rent}">
        <DataGridCheckBoxColumn.HeaderStyle>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
            </Style>
        </DataGridCheckBoxColumn.HeaderStyle>
</DataGridCheckBoxColumn>