在 WPF 中单击按钮时无法 Select 全部在 DataGrid 中 Caliburn.Micro
Can't Select All in DataGrid when Clicking Button in WPF Caliburn.Micro
我正在尝试弄清楚如何使用 bind
button
到 select DataGrid
中的所有 rows
(项目)MVVM
(Caliburn.Micro).
我希望将此按钮与 DataGrid
本身分开。
类似于:
查看:
<Button x:Name="SelectAll"/>
<DataGrid x:Name="People">
<DataGrid.RowStyle>
<Style>
<Setter Property="DataGridRow.IsSelected"
Value="{Binding IsPersonSelected}" />
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Name"
Binding="{Binding PersonName, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
</DataGrid>
视图模型:
public bool _isPersonSelected = false;
public bool IsPersonSelected
{
get { return _isPersonSelected; }
set
{
_isPersonSelected = value;
NotifyOfPropertyChange(() => IsPersonSelected);
}
}
public void SelectAll()
{
if(IsPersonSelected == true)
{
IsPersonSelected = false;
}
else
{
IsPersonSelected = true;
}
}
虽然这不起作用,也许还有另一种方法 selecting DataGrid
中的行使用某种 MVVM 绑定?
或者用某种方式调用 SelectAllCommand
RoutedUICommand
DataGrid
s?
Suggestions/help 将不胜感激。
我没有看到您对 Class 和模型的定义,所以我想您的 IsPersonSelected 是 class 的 属性?你试过吗
public class PersonModel:PropertyChangedBase
{
:
:
public bool IsPersonSelected
{
get => _isPersonSelected;
set
{
_isPersonSelected = value;
NotifyOfPropertyChange(() => IsPersonSelected);
}
}
}
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="IsSelected" Value="{Binding IsPersonSelected, Mode=TwoWay}"/>
</Style>
</DataGrid.RowStyle>
在使用 SelectAll() 之后
你会:
public void SelectAll()
{
foreach(var p in People)
if(p.IsPersonSelected)
//DO something
{
如果您 select 多行,您可以看到该行的值为 true
在您可以通过修改 IsPersonSelected 的值来更改 selection 之后,但不会突出显示数据网格的相应行,属性 IsSelected 不会重现 selected 行的突出显示,要做那是另一个问题(使用 VisualHelper)。要重现由程序编辑的 select 行的突出显示,它稍微复杂一些,需要更改您的编码并且需要拥有完整的代码 wpf。
不使用 属性 IsSelected select 多行的另一种方法:
添加xmlns:cal="http://www.caliburnproject.org"
<DataGrid x:Name="People" cal:Message.Attach="[Event SelectionChanged] = [Row_SelectionChanged($eventArgs)]">
在您的视图模型中:
public void Row_SelectionChanged(SelectionChangedEventArgs obj)
{
//(obj.OriginalSource as DataGrid).SelectedIndex gives you the index of row selected
_selectedObjects.AddRange(obj.AddedItems.Cast<PersonModel>());
obj.RemovedItems.Cast<PersonModel>().ToList().ForEach(w => _selectedObjects.Remove(w));
}
List<PersonModel> _selectedObjects = new List<PersonModel>();
public PersonModel SelectedPeople { get; set; } //Will be set by Caliburn Micro automatically (name convention)
每次你 select 一行,它就会被添加到列表中,你可以使用 ctrl 添加更多行或取消 select 行。每个事件都会修改列表的内容..(对不起我的英语,希望你能理解)。仅供参考,使用 caliburn 你可以访问名称约定,所以就像你的数据网格被命名为 People 一样,第一行 selected 自动绑定到 SelectedPeople
我正在尝试弄清楚如何使用 bind
button
到 select DataGrid
中的所有 rows
(项目)MVVM
(Caliburn.Micro).
我希望将此按钮与 DataGrid
本身分开。
类似于:
查看:
<Button x:Name="SelectAll"/>
<DataGrid x:Name="People">
<DataGrid.RowStyle>
<Style>
<Setter Property="DataGridRow.IsSelected"
Value="{Binding IsPersonSelected}" />
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Name"
Binding="{Binding PersonName, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
</DataGrid>
视图模型:
public bool _isPersonSelected = false;
public bool IsPersonSelected
{
get { return _isPersonSelected; }
set
{
_isPersonSelected = value;
NotifyOfPropertyChange(() => IsPersonSelected);
}
}
public void SelectAll()
{
if(IsPersonSelected == true)
{
IsPersonSelected = false;
}
else
{
IsPersonSelected = true;
}
}
虽然这不起作用,也许还有另一种方法 selecting DataGrid
中的行使用某种 MVVM 绑定?
或者用某种方式调用 SelectAllCommand
RoutedUICommand
DataGrid
s?
Suggestions/help 将不胜感激。
我没有看到您对 Class 和模型的定义,所以我想您的 IsPersonSelected 是 class 的 属性?你试过吗
public class PersonModel:PropertyChangedBase
{
:
:
public bool IsPersonSelected
{
get => _isPersonSelected;
set
{
_isPersonSelected = value;
NotifyOfPropertyChange(() => IsPersonSelected);
}
}
}
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="IsSelected" Value="{Binding IsPersonSelected, Mode=TwoWay}"/>
</Style>
</DataGrid.RowStyle>
在使用 SelectAll() 之后
你会:
public void SelectAll()
{
foreach(var p in People)
if(p.IsPersonSelected)
//DO something
{
如果您 select 多行,您可以看到该行的值为 true 在您可以通过修改 IsPersonSelected 的值来更改 selection 之后,但不会突出显示数据网格的相应行,属性 IsSelected 不会重现 selected 行的突出显示,要做那是另一个问题(使用 VisualHelper)。要重现由程序编辑的 select 行的突出显示,它稍微复杂一些,需要更改您的编码并且需要拥有完整的代码 wpf。
不使用 属性 IsSelected select 多行的另一种方法:
添加xmlns:cal="http://www.caliburnproject.org"
<DataGrid x:Name="People" cal:Message.Attach="[Event SelectionChanged] = [Row_SelectionChanged($eventArgs)]">
在您的视图模型中:
public void Row_SelectionChanged(SelectionChangedEventArgs obj)
{
//(obj.OriginalSource as DataGrid).SelectedIndex gives you the index of row selected
_selectedObjects.AddRange(obj.AddedItems.Cast<PersonModel>());
obj.RemovedItems.Cast<PersonModel>().ToList().ForEach(w => _selectedObjects.Remove(w));
}
List<PersonModel> _selectedObjects = new List<PersonModel>();
public PersonModel SelectedPeople { get; set; } //Will be set by Caliburn Micro automatically (name convention)
每次你 select 一行,它就会被添加到列表中,你可以使用 ctrl 添加更多行或取消 select 行。每个事件都会修改列表的内容..(对不起我的英语,希望你能理解)。仅供参考,使用 caliburn 你可以访问名称约定,所以就像你的数据网格被命名为 People 一样,第一行 selected 自动绑定到 SelectedPeople