我在 DataGrid(WPF Toolkit)中绑定 SelectedItem 时遇到问题。当我从主窗体 SelectedItem 打开 UserControl 时,DataGrid 中不显示。但是如果在调试器中查看,一切都很好,并且 selecteditem 有一些价值。然后例如,如果我再次设置值 SelectedItem(在代码中),DataGrid 开始正确显示 SelectedItem。
这是我的代码的一部分:
DictionaryView.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>
字典视图模型
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 的深蓝线。
PS对不起我的英语不好。