我在DataGrid(WPF Toolkit)中绑定SelectedItem时遇到问题。当我从主窗体中打开UserControl时,SelectedItem不显示在DataGrid中。但是如果要在调试器中查看,那么一切都是正确的,并且selecteditem具有一定的价值。然后,例如,如果我再次设置值SelectedItem(在代码中),DataGrid开始正确显示SelectedItem。DataGrid中的SelectedItem MVVM Light
这里是我的代码的一部分:
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>
DictionaryViewModel
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深蓝色线。
P.S.对不起,我的英语不好。
刚刚编辑我的回复现在我可以看到你在做什么 – SteveL