在WPF Caliburn.Micro中点击按钮时,无法在DataGrid中全部选择。

问题描述 投票:0回答:1

我想知道如何 bind a button 选择所有 rows归入 DataGrid 使用 MVVM(Caliburn.Micro)。

我想让这个按钮与这个功能分开。DataGrid 本身。

类似的东西。

View:

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

ViewModel:

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

但这是行不通的 也许还有其他的方法可以选择行 DataGrid 使用MVVM的某种绑定?

或者以某种方式调用 SelectAllCommand RoutedUICommand 对于 DataGrids?

Suggestionshelp would be greatly appreciated.I'm trying to figure out how to bind a button to select all rows( items) in a DataGrid using MVVM(Caliburn.Micro).

c# wpf mvvm datagrid caliburn.micro
1个回答
1
投票

我没有看到你的类和模型的定义,所以我想你有IsPersonSelected是你的类的属性吗?

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
      {

如果你选择了多行,你可以看到这行的值在true之后,你可以通过修改IsPersonSelected的值来改变选择,但这并不能突出数据表格中相应的行,IsSelected属性并不能再现所选行的高光,要做到这一点,这是另一个问题(使用VisualHelper)。要重现程序所选行的高亮显示,这有点复杂,需要改变你的编码,并需要有你完整的代码Wpf。


另一种方法是在不使用IsSelected属性的情况下选择多行。

添加 xmlns:cal="http://www.caliburnproject.org"

    <DataGrid x:Name="People" cal:Message.Attach="[Event SelectionChanged] = [Row_SelectionChanged($eventArgs)]">

在你的视图模型中,每次你选择一行,它就会被添加到列表中,用ctrl可以添加更多的行或取消选择。

    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)

每当你选择一行,它就会被添加到列表中,用ctrl可以添加更多的行或取消选择。每个事件都会修改列表的内容。(对不起我的英语,希望你能理解)。顺便说一下,使用caliburn你可以访问名称约定,所以就像你的数据网格被命名为People一样,被选择的第一行会自动绑定到SelectedPeople上。

© www.soinside.com 2019 - 2024. All rights reserved.