wpfdatagrid - DataGrid MVVM Light 中的 SelectedItem

标签 wpfdatagrid wpftoolkit selecteditem

我在 DataGrid(WPF 工具包)中绑定(bind) SelectedItem 时遇到问题。当我从主窗体 SelectedItem 打开 UserControl 时,DataGrid 中没有显示。但是如果在调试器中查看,一切都很好,并且 selecteditem 有一些值(value)。然后,例如,如果我再次设置值 SelectedItem(在代码中),DataGrid 开始正确显示 SelectedItem。

这是我的代码的一部分:

字典 View .xaml

<UserControl x:Class="AccountingCatridge.Views.DictionaryView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" 
         xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
         DataContext="{Binding Source={StaticResource Locator}, Path=Dictionary}">
<Grid>
    <Label Content="{Binding Title}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="300" FontWeight="Bold" HorizontalContentAlignment="Center" FontSize="14" />
    <my:DataGrid x:Name="dataGrid"
                 Height="247" 
                 HorizontalAlignment="Left" 
                 Margin="0,53,0,0" 
                 VerticalAlignment="Top"
                 Width="300" 
                 IsReadOnly="True"
                 AutoGenerateColumns="False"
                 ItemsSource="{Binding DataItems}" 
                 SelectedItem="{Binding SelectedDictionaryItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                 HeadersVisibility="None">
        <my:DataGrid.Columns>
            <my:DataGridTextColumn Header="ID" Width="50" Binding="{Binding ID}" IsReadOnly="True"/>
            <my:DataGridTextColumn Header="Value" Width="*" Binding="{Binding Value}" IsReadOnly="True" />
        </my:DataGrid.Columns>

    </my:DataGrid>
    <ToolBarTray Height="26" HorizontalAlignment="Left" Margin="0,27,0,0" Name="toolBarTray1" VerticalAlignment="Top" Width="300" IsLocked="True">
        <ToolBar>
            <ToolBar.Items>
                <Button Content="Add" Command="{Binding AddElementCommand}"/>
                <Button Content="Edit" Command="{Binding EditElementCommand}"/>
                <Button Content="Delete" Command="{Binding DeleteElementsCommand}"/>
            </ToolBar.Items>
        </ToolBar>
    </ToolBarTray>
</Grid>

字典 View 模型

public DictionaryRecord SelectedDictionaryItem
{
    get { return _selectedDictionaryItem; }
    set
    {
        if (_selectedDictionaryItem == value) return;
        _selectedDictionaryItem = value;
        RaisePropertyChanged("SelectedDictionaryItem");
    }

}

public IEnumerable<DictionaryRecord> DataItems
{
    get { return _dataItems; } 
    set
    {
        if (_dataItems == value) return;
        _dataItems = value;
        RaisePropertyChanged("DataItems");
        SelectedDictionaryItem = _dataItems.First();
    }
}

public DictionaryViewModel(IDataService dataService)
{
    _dataService = dataService;
    Messenger.Default.Register<ShowDictionaryMessage>(this, ShowDictionary);
}

private void ShowDictionary(ShowDictionaryMessage mes)
{
    _typeDict = mes.TypeDict;
    Title = StringEnum.GetStringValue(_typeDict);

    switch (_typeDict)
    {
        case TypeDictionary.Employees:
            DataItems = _dataService.GetEmployees();
            break;
        case TypeDictionary.ModelPrinters:
            DataItems = _dataService.GetPrinters();
            break;
    }
}

它是更多的代码和图像。

    public RelayCommand EditElementCommand
        {
            get { return _editElementCommand ?? (_editElementCommand = new RelayCommand(EditElement)); }
        }

        private void EditElement()
        {
            if (SelectedDictionaryItem == null) return;
            Messenger.Default.Send(new ShowDictionaryRecordMessage{ Action = TypeRecordAction.Edit, Dictionary = _typeDict, Record = SelectedDictionaryItem});
        }

public RelayCommand SomeSimpleCommand
        {
            get { return _someSimpleCommand ?? (_someSimpleCommand = new RelayCommand(SomeSimpleAction)); }
        }

        private void SomeSimpleAction()
        {
            SelectedDictionaryItem = _dataItems.Last();
        }

这里是我打开表格时看到的

........

但 SelectedDictionaryItem 具有值,如果我按下“编辑”,包含我所需数据的表单将正确打开。

如果我执行 SomeSimpleCommand。我看到以下内容

........

我需要知道为什么在第一种情况下我没有看到 SelectedItem 的深蓝色线。

附言抱歉我的英语不好。

最佳答案

编辑: 好的 - 可以看到您现在正在尝试做什么...

我过去也遇到过类似的问题,尽管那是与第 3 方控件有关。我想我遇到的是在数据加载到网格之前设置了 Selected 项目。

我最终不得不做一些软糖,在网格的数据加载事件中隐藏了一些代码。 vm 是一个模块级变量,它存储对 ViewModel 的引用 - 或者你可以通过 this.DataContext 作为 ViewModel 获取它,因为你似乎使用定位器来获取你的:

 private void CaseGridView_DataLoaded(object sender, EventArgs e) 
    { 
        var grid = sender as GridView; 
        if (grid != null) 
        { 
            grid.SelectedItem = vm.CurrentlySelectedItem; 
        } 
    } 

另一件值得尝试的事情是 UpdateLayout() 以确保它刷新

关于wpfdatagrid - DataGrid MVVM Light 中的 SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12818610/

相关文章:

c# - .NET 4.0 WPF,带行和列标题的 DataGrid

c# - WPF DataGrid 上下文菜单绑定(bind)

C#/WPF - 重新编译 SilverLight 4 工具包以用于 WPF 应用程序是否可行?

JavaFX TabPane : How to set the selected tab

WPF DataGrid 最佳实践?

c# - WPF 数据网格行数据绑定(bind)?

WPF 工具包 DataGrid 多选 : How to get SelectedItems out?

c# - 如何在应用程序外壳中实现繁忙指示器

c# - 数据绑定(bind)到 WPF TreeView 中的 SelectedItem

c# - 为什么这是一个无效的类型转换?