是否有可以监听的事件来检查当前的编辑状态?当组合框未处于编辑模式时,如何监听。
忽略颜色,重点是 ComboBox 处于用户可以编辑/写入文本或不可以编辑/写入文本的模式。焦点不起作用,因为当组合框不在焦点上时它仍然可以处于此模式。
我有一个对象 DataSet,它具有 name 属性和 ID 属性。我的视图模型中有一个数据集列表,它应该是 DataGrid Combobox 列的 itemsource。我已定义绑定如下:
dataGridControl:DataGridComboBoxColumn
Binding="{Binding DataSetIndex}"
Header="DataSet"
ItemsSource="{x:Bind Model.DataSets}"
DisplayMemberPath="Name"
Tag="DataSet" />
现在,这可以正常工作,除了我需要以某种方式告诉绑定它应该使用 DataSet.ID 属性。因此,当用户在组合框中选择一个值时,绑定 DataSetIndex 应该设置为 int,即 DataSet.ID 属性。我如何在绑定中执行此操作?现在我当然会收到一条错误消息,提示 itemsource 没有 int 属性,因为它试图将 DataSet 对象作为一个整体使用。现在,社区工具包中没有 SelectedValuePath 吗?我必须使用 templatecolumn 吗?
这是我根据您的解决方案更新的问题。它还具有代码隐藏绑定。
<dataGridControl:DataGridTemplateColumn Header="DataSet">
<dataGridControl:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate x:DataType="local:DataExtractionSetupViewModel">
<ComboBox x:Name="dataSetsComboBox" Loaded="ComboBox_Loaded"
DisplayMemberPath="Name"
SelectedValue="{Binding DataSetIndex, Mode=TwoWay}" SelectedValuePath="ID" IsDropDownOpen="True"/>
</DataTemplate>
</dataGridControl:DataGridTemplateColumn.CellEditingTemplate>
<dataGridControl:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DatasetIndex}"/>
</DataTemplate>
</dataGridControl:DataGridTemplateColumn.CellTemplate>
</dataGridControl:DataGridTemplateColumn>
现在我唯一的问题是,我们如何才能使这个自定义模板列看起来与普通的数据网格组合框列完全一样,并且在文本框中显示数据集名称而不是索引?
如何在隐藏代码中以编程方式在显示成员路径上使用转换器或将 itemtemplate 与转换器一起使用?
var listView = new ListView
{
ItemsSource = SomeObject,
HorizontalAlignment = HorizontalAlignment.Right,
SelectionMode=ListViewSelectionMode.Single,
DisplayMemberPath=<---use converter here
};
var stackPanel = new StackPanel();
stackPanel.Children.Add(listView);
等效的 xaml 示例供参考:
<ListView ItemsSource="{x:Bind Object, Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource Converter}}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在数据网格上下文中,所选项目有双向绑定,如果选择了多行,如何防止 g.cs 代码更新绑定?
<dataGridControl:DataGrid
SelectedItem="{x:Bind Model.SelectedItem, Mode=TwoWay}"
在用户选择多行的情况下,当您选择多行时,它将使用最后选择的行,当然,考虑到它是双向绑定,您不能在不清除整个 DataGrid 选择的情况下清除视图模型中的选定项属性。考虑到选择了多行而不是最后一行,视图模型中的选定项具有误导性。
我想根据数据网格的选定项数获得可见性。但是,这在 WinUI 中似乎不起作用,但在 WPF 中却可以正常工作?
转换器:
public object Convert(object value, Type targetType, object parameter, string language)
{
if ((int)value == 1)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
裝備:
Visibility="{Binding ElementName=DataGrid,Path=SelectedItems.Count,
Converter={StaticResource Converter}}"
还尝试使用 x:Bind:
Visibility="{x:Bind DataGrid.SelectedItems.Count,
Converter={StaticResource CountToVisibilityConverter}}"
我觉得该属性没有更新。如果我在转换器中设置断点,它只会在初始阶段被调用。
使用 Community ToolKit 中的 DataGrid。如何绑定事件处理程序来监听 DataGridComboBoxColumn 中的更改?
在 mvvm 中创建可观察属性的正确方法是
[ObservableProperty]
private string _name;
但是,在某些情况下,您需要更改集合。如何在不声明 Public 的情况下正确执行此操作?
private double _currentProgressPct;
public double CurrentProgressPct
{
get => _currentProgressPct;
set
{
_dispatcherQueue.TryEnqueue(DispatcherQueuePriority.High, () =>
{
SetProperty(ref _currentProgressPct, value);
});
}
}
据我了解,根据 mvvm,第一种方法是正确的,但后者不正确?
另外,根据本文,您需要实现 INotifyPropertyChanged 接口。但是,使用 [ObservableProperty] 时这是否必要?据我了解,社区工具包需要该类为可观察对象?
我之所以问最后几个问题,是因为使用具有内存焦点的性能分析器检查时,绑定似乎没有被释放。根据另一个线程,我发现这可能是因为未实现 INotifyProperyChanged?
我试图让用户在数据网格的组合框中进行选择,但找不到正确的方法。我尝试过使用绑定和 x:bind。最好的方法是什么?我认为以下代码是正确的,但我遗漏了一些东西。
视图模型
public partial class CustomerModel : ObservableObject
{
private ObservableCollection<DataType> _objects;
public ObservableCollection<DataType> Objects
{
get => _objects;
set => SetProperty(ref _objects, value);
}
private ObservableCollection<string> _field;
public ObservableCollection<string> Field
{
get => _field;
set => SetProperty(ref _field, value);
}
public CustomerModel()
{
Objects = new();
Field = new ObservableCollection<string> { "Test", "Test" };
}
public class DataType : ObservableObject
{
public int ID { get; set; }
public string Name { get; set; }
public string Caption { get; set; }
public bool Numeric { get; set; }
public DataType()
{
}
}
}
.xaml
<controls:DataGrid
x:Name="dataGrid1" KeyDown="dataGrid1_KeyDown"
ItemsSource="{x:Bind Model.Objects}" AutoGenerateColumns="False" >
<controls:DataGrid.Columns>
<controls:DataGridComboBoxColumn Header="Field" ItemsSource="{x:Bind Model.Field}" Tag="Field"/>
</controls:DataGrid.Columns>
</controls:DataGrid>
更新后的代码现在可正常工作。看起来还好吗?
public partial class CustomerModel : ObservableObject
{
public enum Choices { A, B, C }
[ObservableProperty]
private ObservableCollection<DataType> _objects;
public CustomerModel()
{
Objects = new();
var data = new DataType();
Objects.Add(data);
}
public static Choices[] ChoicesOptions { get; } = Enum.GetValues<Choices>();
public partial class DataType : ObservableObject
{
public int ID { get; set; }
public string Name { get; set; }
public string Caption { get; set; }
public bool Numeric { get; set; }
[ObservableProperty]
private Choices _choice;
}
}
.xaml
<controls:DataGridComboBoxColumn Header="Field" Binding="{Binding Choice}" ItemsSource="{x:Bind local:CustomerModel.ChoicesOptions, Mode=OneTime}" Tag="Field"
另外,我注意到您在代码隐藏文件中实例化了视图模型,这有必要吗?我没有:
public sealed partial class DataGridPage : Page
{
public DataGridPage()
{
this.InitializeComponent();
}
}