DataGrid (WPF Toolkit)에서 SelectedItem을 바인딩 할 때 문제가 있습니다. 주 양식 SelectedItem에서 UserControl을 열면 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>
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;
}
}
조금 더 많은 코드와 이미지입니다 DictionaryViewModel
: 여기 내 코드의 일부입니다. 나는 형태을 여는하지만 SelectedDictionaryItem 값을 가질 때보고 나는 "편집"나를 위해 필요한 데이터와 양식을 누를 경우 제대로 열립니다 여기
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();
}
.
SomeSimpleCommand를 실행하면. 내가 보는 내가 첫 번째 경우에 나는의 selectedItem에 대한 어두운 파란색 선이 표시되지 않는 이유를 알 필요가
다음과 같습니다.
P. 내 하찮은 영어 실력에 죄송하다는 말씀을 드리고 싶습니다.
방금 편집하신 내 답변입니다. – SteveL