5

以下のサンプルより簡単なものはありますか? DataGrid lstLinks にバインドされた監視可能なコレクション (コード内の「リスト」) があります。

for (int i = 0; i < list.Count ; i++)
{
    object rowItem = lstLinks.Items[i] ; 
    DataGridRow visualItem =  (DataGridRow)lstLinks.ItemContainerGenerator.ContainerFromItem(rowItem);
    if ( visualItem == null ) break;  
    if (list[i].Changed)
        visualItem.IsSelected = false;
    else
         visualItem.IsSelected = false; 

}
4

4 に答える 4

9

サラム・マイクミット:)

ええ、もっと簡単な解決策があります。バインドされたリストから必要なアイテムを DataGrid SelectedItems コレクションに追加するだけです。以下のコードを参照してください: [この投稿で問題が解決した場合は、回答としてマークすることを忘れないでください:) ]

<Window x:Class="ProgGridSelection.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="OnWindowLoaded">
<StackPanel>
    <DataGrid Name="empDataGrid" ItemsSource="{Binding}" Height="200"/>
    <TextBox Name="empNameTextBox"/>
    <Button Content="Click" Click="OnSelectionButtonClick" />
</StackPanel>

public partial class MainWindow : Window
{
    public class Employee
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }

    private ObservableCollection<Employee> _empCollection;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void OnWindowLoaded(object sender, RoutedEventArgs e)
    {
        // Generate test data
        _empCollection =
            new ObservableCollection<Employee>
                {
                    new Employee {Code = "E001", Name = "Mohammed A. Fadil"},
                    new Employee {Code = "E013", Name = "Ahmed Yousif"},
                    new Employee {Code = "E431", Name = "Jasmin Kamal"},
                    new Employee {Code = "E431", Name = "Zuhair Zein"},
                    new Employee {Code = "E431", Name = "Layla Abdullah"},
                };

        /* Set the Window.DataContext, alternatively you can set your
         * DataGrid DataContext property to the employees collection.
         * on the other hand, you you have to bind your DataGrid
         * DataContext property to the DataContext (see the XAML code)
         */
        DataContext = _empCollection;
    }

    private void OnSelectionButtonClick(object sender, RoutedEventArgs e)
    {
        /* select the employee that his name matches the
         * name on the TextBox
         */
        var emp = (from i in _empCollection
                   where i.Name == empNameTextBox.Text.Trim()
                   select i).FirstOrDefault();

        /* Now, add it to your DataGrid SelectedItems collection to
         * add the item to the selected rows
         */
        if (emp != null)
            empDataGrid.SelectedItems.Add(emp);
    }
}
于 2011-03-30T08:17:30.783 に答える
1

MVVM より柔軟なソリューション
が必要ですが、これは MVVM であり、完全にテスト可能で保守が容易です。

こちらをご覧ください MVVMで複数の選択を管理する

于 2012-06-21T14:44:54.780 に答える
0

ええ、このソリューションは DataGrid コントロールをハッキングするよりも優れています。1 行だけを選択する場合は、次のコードも使用できます。

myDataGrid.SelectedItem = item;

item は、DataGrid にバインドされたアイテムの 1 つです。

于 2011-03-30T13:17:39.030 に答える