Skip to main content
 首页 » 编程设计

c#之如何在绑定(bind) WPF DataGrid 后修改单元格

2024年02月06日29sky-heaven

我正在尝试更新 WPF 数据网格中单元格的外观,我来自 ASP.NET Webforms,但我找不到 DataBound 或类似的东西,我可以利用它来进行更改。这在 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. 
  } 

这是复选框的标记,基本上我想启用 Buy,如果绑定(bind)值为 true,如果为 true,则禁用 Rent,如果 Rent 为 true,则禁用 Buy

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

请您参考如下方法:

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

请看下面的例子,两个复选框启用和禁用两个按钮。显然,这两个复选框不需要可见;它们与您的数据数据绑定(bind)。如果您需要 bool 值之间的复杂逻辑,那么您可以在代码中执行此操作,或使用转换器根据上下文设置每个项目的可见性。

查看:

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