【问题标题】:How to get row selection working with DataGrid using XAML only databinding如何使用仅使用 XAML 数据绑定的 DataGrid 进行行选择
【发布时间】:2013-08-08 06:25:34
【问题描述】:

我有以下 DataGrid 定义

 <DataGrid
    VirtualizingPanel.VirtualizationMode="Recycling"
    CanUserAddRows="False"
    CanUserDeleteRows="False"
    SelectionMode="Single"
    ItemsSource="{Binding 
        ToolPath.ToolPath, 
        Mode=OneWay,
        Converter={StaticResource indexedConverter}}" 
    AutoGenerateColumns="False" 
    SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}">

我正在使用 MVVM,并且有一个带有 SelectedIndex 属性的视图模型来实现 INPC。我使用 snoop 来验证 DataGrid 上的 SelectedIndex 在视图模型中发生变化时是否发生了变化。但是,当我这样做时,不会选择或突出显示网格的行。

如您所见,该行未突出显示,但我想我可以检测到该行左侧的 3 像素宽小部件是深蓝色。

是否有 XAML only 方式 via databinding ( without writing code behind ) 让行选择工作?

【问题讨论】:

  • 你能粘贴IndexedConverter的代码吗?为什么不只是绑定到集合并使用 SelectedItem?

标签: c# wpf datagrid


【解决方案1】:

似乎无法从 XAML 中找出方法。有了很多黑客和阅读帖子,我能想到的最好的就是。

<DataGrid
    x:Name="ToolPathGridView"
    VirtualizingPanel.VirtualizationMode="Recycling"
    CanUserAddRows="False"
    SelectionUnit="FullRow"
    CanUserDeleteRows="False"
    SelectionMode="Single"
    Loaded="ToolPathGridView_Loaded"
    ItemsSource="{Binding 
        ToolPath.ToolPath, 
        Mode=OneWay,
        Converter={StaticResource indexedConverter}}" 
    AutoGenerateColumns="False" 
    SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}">

和

private void ToolPathGridView_Loaded( object sender, RoutedEventArgs e )
{
    ToolPathGridView.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
}
 void ItemContainerGenerator_StatusChanged( object sender, EventArgs e )
{
    if ( ToolPathGridView.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated )
    {
        int i = 0;
        if ( ViewModel!=null )
        {
            i = ViewModel.SelectedIndex;
        }
        ToolPathGridView.SelectedIndex = 0;
        DataGridRow r = ToolPathGridView.ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
        if ( r != null )
        {
            r.IsSelected = false;
            r.IsSelected = true;
        }
    }
}

所以这里发生了什么。诀窍似乎是你必须等到数据网格建立它的内部容器模型。完成后,您可以选择一行并将其标记为选中。如果您尝试过早执行此操作,则没有行。

这确实应该是网格本身的属性。

【讨论】:

    【解决方案2】:

    使用来自NuGet 的MVVM light 的工作示例。

    您可以在下方的文本框中写入索引,以查看它在 DataGrid 上的变化。

    MainWindow.xaml

    <Window
        x:Class="Test_DataGridSelectedIndexItem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:testDataGridSelectedIndexItem="clr-namespace:Test_DataGridSelectedIndexItem"
        Title="MainWindow"
        Height="350"
        Width="525"
        >
        <Window.DataContext>
            <testDataGridSelectedIndexItem:MainViewModel />
        </Window.DataContext>
    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height=".9*"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
    
            <DataGrid
                Grid.Row="0"
                ItemsSource="{Binding Items}"
                SelectedItem="{Binding Selected, Mode=TwoWay}"
                SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
                />
    
            <TextBlock
                Grid.Row="1"
                Text="{Binding Selected}"
                />
    
            <TextBlock
                Grid.Row="2"
                Text="{Binding SelectedIndex}"
                />
    
            <TextBox
                Grid.Row="3"
                Text="{Binding SelectedIndex, Mode=TwoWay}"
                />
        </Grid>
    </Window>
    

    MainViewModel.cs

    using System.Collections.ObjectModel;
    using GalaSoft.MvvmLight;
    
    namespace Test_DataGridSelectedIndexItem
    {
        public class MainViewModel : ViewModelBase
        {
            private ItemViewMode selected;
            private int selectedIndex;
    
            public MainViewModel()
            {
                this.Items = new ObservableCollection<ItemViewMode>()
                             {
                                 new ItemViewMode("Item1"),
                                 new ItemViewMode("Item2"),
                                 new ItemViewMode("Item3"),
                             };
            }
    
            public ObservableCollection<ItemViewMode> Items { get; private set; }
    
            public ItemViewMode Selected
            {
                get
                {
                    return this.selected;
                }
                set
                {
                    this.selected = value;
                    this.RaisePropertyChanged(() => this.Selected);
                }
            }
    
            public int SelectedIndex
            {
                get
                {
                    return this.selectedIndex;
                }
                set
                {
                    this.selectedIndex = value;
                    this.RaisePropertyChanged(() => this.SelectedIndex);
                }
            }
        }
    }
    

    ItemViewModel.cs

    using GalaSoft.MvvmLight;
    
    namespace Test_DataGridSelectedIndexItem
    {
        public class ItemViewModel : ViewModelBase
        {
            private string text;
    
            public ItemViewModel() : this(string.Empty)
            {
            }
    
            public ItemViewModel(string text)
            {
                this.text = text;
            }
    
            public string Text
            {
                get
                {
                    return this.text;
                }
                set
                {
                    this.text = value;
                    this.RaisePropertyChanged(() => this.Text);
                }
            }
    
            public override string ToString()
            {
                return this.text;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 2016-04-14
      • 1970-01-01
      相关资源
      最近更新 更多