- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试更新 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;
关于c# - 如何在绑定(bind) WPF DataGrid 后修改单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25050129/
我正在使用 Avalonia.Controls.DataGrid。默认情况下,当网格获得焦点并按下 Enter 时,它会自动处理事件并将选择移动到下一项。我怎样才能防止这种默认行为?我想要一个自定义的
根据我所见,Dojo DataGrid 中的格式化程序函数被赋予以下参数:单元格值、单元格行号和单元格对象本身。鉴于这些参数,您能否建议如何获取此单元格所引用的数据存储项?或者,如果您能提出替代方法,
我是DoJo开发的新手,所以这可能很基础。 我创建了一个EnhancedDatagrid,它可以很好地显示数据。 数据来自另一个页面中的JSON存储。 我有一个按钮,该按钮导致在数据存储中创建一个新条
我正在尝试在 WPF DataGrid 的 RowDetailsTemplate 中创建一个 DataGrid。 我有一系列工作。每个工作都可以分配一个或多个员工: public class Job
我有一个数据网格组件: [... some controlls ...]
我已经以编程方式创建了一个dojox.grid.datagrid,并且我需要对列进行自定义排序。为此,我尝试使用 ItemFileWriteStore.comparatorMap['field'] =
我试图在 UWP RadDataGrid 控件的 Telerik UI 中隐藏 GroupPanel。 radgrid.ShowGroupPanel 不起作用。 最佳答案 UserGroupMode=
我收到“在使用 ItemsSource 时操作无效。改为使用 ItemsControl.ItemsSource 访问和修改元素”错误,我不确定原因。 我正在尝试将数据网格嵌套在数据网格中。如果我只使用
我的 WPF forme Etudiant 中有一个 DataGrid 当单击 Etudiant 中名为 Epreuve 的行时,我需要显示其他数据网格,并且当在 Epreuve 中选择一行并且我需要
我有一个 DataGrid“嵌套”在另一个 DataGrid 的 RowDetailsTemplate 中。只要我的鼠标悬停在父 DataGrid 上行的主要部分上,滚动就可以正常工作,但是当鼠标悬停
我花了几个小时看这个没有结果。 我只是想要一个DataGrid X 列保持与 Grid 的相对宽度本身。 因此,例如: 第 1 栏:10% 第 2 栏:10% 第 3 栏:10% 我设置了一个附加到
我有 3 个共享相同数据类型的数据网格。我想配置一次列绑定(bind)并让 3 个数据网格共享资源。 例如
我之前关于检测 VM 中的属性更改的帖子不够深入,所以我发布了这个 我有一个工作网格。每个工作可以有一个或多个员工。 DataGrid 的 RowDetailsTemplate 包含另一个用于显示员工
我将尝试找出将 ComboBox 嵌入到 Flex (3.4) DataGrid 中的“正确”方法。根据权利(例如,根据本页 http://blog.flexmonkeypatches.com/200
我有一个对象集合,我们称之为 People,每个对象都由名称、ID 和时间字符串组成。这些人物必须显示在类似于固定行数和 9 列的网格上。这个想法是将人员作为行添加到第 1、2 和 3 列中的网格左侧
默认行为是使用 CTRL+Click 取消选择 Datagrid 中的项目 我希望能够通过鼠标单击(左键或右键)网格中的空白区域并让它取消选择任何选定的项目。 我已经用谷歌搜索死了,发现了一些非常复杂
我正在使用 DataGrid来自 WPF Toolkit我需要能够将注意力集中在网格的底部(即最后一行)。我现在遇到的问题是,随着行的添加,DataGrid 的滚动条不会随着新行的添加而滚动。实现此目
我正在使用 DataGrid 来管理服务配置。我想使用自动生成的行复选框来管理多个删除操作,但想使用 onRowClick 事件将行数据提供给模态对话框表单进行编辑。我的 onRowClick 处理程
我有一个包含项目的 DataGrid。当您右键单击其中一行时,会显示一个 Dojo 上下文菜单,其中包含删除该行的选项。如果您尝试右键单击 DataGrid 的空白区域,则不会显示上下文菜单……但是,
我想在另一个 Datagrid 的 RowDetailsTempalte 中使用一个 DataGrid。此内部 Datagrid 应将其列绑定(bind)到外部 Datagrid 中当前对象的属性。例
我是一名优秀的程序员,十分优秀!