簡體   English   中英

單擊 WPF Caliburn.Micro 中的按鈕時,無法 Select All in DataGrid

[英]Can't Select All in DataGrid when Clicking Button in WPF Caliburn.Micro

我試圖弄清楚如何使用MVVM (Caliburn.Micro)將button bind到select DataGrid中的所有rows (項目)。

我希望這個按鈕與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;
    }
}

但這不起作用,也許還有另一種方法可以使用某種針對 MVVM 的綁定來選擇DataGrid中的行?

或者以某種方式調用DataGridRoutedUICommand SelectAllCommand

建議/幫助將不勝感激。

我沒有看到您對 Class 和 Model 的定義,所以我想您的 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 的值來更改選擇后,但不會突出顯示數據網格的相應行,屬性 IsSelected 不會重現所選行的突出顯示,要做到這一點,這是另一個問題(使用 VisualHelper)。 要重現程序選擇的行的突出顯示,它有點復雜,需要更改您的編碼,並且需要您的完整代碼 wpf。


select 多行而不使用屬性 IsSelected 的另一種方法:

添加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 您可以添加更多行或取消選擇一個。 每個事件都會修改列表的內容..(對不起我的英語,希望你能理解)。 僅供參考,使用 caliburn 您可以訪問名稱約定,因此就像您的數據網格被命名為 People,選擇的第一行自動綁定到 SelectedPeople

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM