我在DataGrid(WPF Toolkit)中绑定SelectedItem时遇到问题。当我从主窗体打开UserControl时,SelectedItem不会显示在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>
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。抱歉我的英语不好。
答案 0 :(得分:0)
二叔: 好的 - 可以看看你现在要做什么......
过去我遇到过类似的问题,尽管那是第三方控制。我认为我遇到的是在数据加载到网格之前设置了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()以确保它刷新