我有一个绑定到DataGrid的MeetingViewModelList。每个MeetingViewModel都有一个DocumentViewModelList绑定到DataGrid 的DataGridTemplateColumn内的ListBox。
DocumentViewModel 的 IsSelected 属性绑定到 ListBox 的 Item 属性 IsSelected。
我在输出控制台中没有遇到绑定错误。
DocumentViewModel 中的删除文档按钮在其 CanExecute 方法中检查:
private bool CanDeleteDocument()
{
return _isSelected;
}
当我在 ListBox 中选择第一个项目时,删除按钮被启用。当我选择列表框中的第 2、3 等项时,删除按钮始终处于禁用状态。
我尝试仅粘贴重要代码并裁剪其他内容:
我刚刚尝试仅使用 ListBox(不是 DataGrid 的一部分)来重建场景,但我得到了相同的行为:/
我会很高兴任何提示:)
XAML:
<DataGrid VirtualizingStackPanel.VirtualizationMode="Recycling"
ScrollViewer.CanContentScroll="False"
CanUserResizeRows="True"
VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding MeetingViewModelList}"
AutoGenerateColumns="False"
x:Name="DailyGrid"
Height="580"
SelectionMode="Single"
CanUserSortColumns="False"
Background="#FF2DCE2D"
CanUserAddRows="False"
HeadersVisibility="All"
RowHeaderWidth="40"
RowHeight="200" >
<!--Content-->
<DataGridTemplateColumn Width="0.5*" Header="Content">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Helper:RichTextBox LostFocus="RTFBox_LostFocus" VerticalScrollBarVisibility="Auto" x:Name="RTFBox" Text="{Binding Content,IsAsync=True}" AcceptsReturn="True" AutoWordSelection="False" AllowDrop="False" SelectionBrush="#FFAC5BCB" HorizontalScrollBarVisibility="Hidden">
<Helper:RichTextBox.TextFormatter>
<Helper:RtfFormatter />
</Helper:RichTextBox.TextFormatter>
</Helper:RichTextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--Documents-->
<DataGridTemplateColumn Visibility="{Binding Source={StaticResource spy}, Path=DataContext.DocumentsVisible}" IsReadOnly="True" Width="125" Header="Attachments">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Background="Green" DataContext="{Binding DocumentViewModelList}" Orientation="Vertical" >
<ListBox SelectionMode="Single" VirtualizingStackPanel.IsVirtualizing="False"
Height="100"
Width="Auto"
Focusable="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Grid.Row="1"
Name="documentListBox"
BorderThickness="1"
ItemsSource="{Binding}"
Visibility="{Binding ElementName=documentListBox,Path=HasItems, Converter={StaticResource boolToVisibilityConverter}}"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<Button Command="{Binding Path=DeleteDocumentCommand}" HorizontalAlignment="Stretch" Content="Delete" />
<Button Command="{Binding Path=AddDocumentCommand}" HorizontalAlignment="Stretch" Content="Add" />
<Button Command="{Binding Path=OpenDocumentCommand}" HorizontalAlignment="Stretch" Content="Open" />
</StackPanel>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
报告视图模型(控制器):
public class ReportingViewModel : ViewModelBase
{
private ObservableCollection<MeetingViewModel> _meetingViewModelList;
public ReportingViewModel ()
{
}
public ObservableCollection<MeetingViewModel> MeetingViewModelList
{
get { return _meetingViewModelList; }
set
{
_meetingViewModelList= value;
this.RaisePropertyChanged("MeetingViewModelList");
}
}
}
会议视图模型:
public class MeetingViewModel: ViewModelBase
{
private ObservableCollection<DocumentViewModel> _documentViewModelList = new ObservableCollection<DocumentViewModel>();
private Meeting _meeting;
public MeetingViewModel(Meeting meeting)
{
_meeting= meeting;
_meeting.Documents.ForEach(doc => DocumentViewModelList.Add(new DocumentViewModel(doc)));
}
public ObservableCollection<DocumentViewModel> DocumentViewModelList
{
get { return _documentViewModelList; }
set
{
_documentViewModelList = value;
this.RaisePropertyChanged("DocumentViewModelList");
}
}
public string Content
{
get { return _meeting.Content; }
set
{
if (_meeting.Content == value)
return;
_meeting.Content = value;
this.RaisePropertyChanged("Content");
}
}
}
文档视图模型:
public class DocumentViewModel : ViewModelBase
{
private Document _document;
private RelayCommand _deleteDocumentCommand;
private RelayCommand _addDocumentCommand;
private RelayCommand _openDocumentCommand;
public DocumentViewModel(Document document)
{
_document = document;
}
private void DeleteDocument()
{
throw new NotImplementedException();
}
private bool CanDeleteDocument()
{
return _isSelected;
}
private void AddDocument()
{
}
private void OpenDocument()
{
}
public RelayCommand DeleteDocumentCommand
{
get { return _deleteDocumentCommand ?? (_deleteDocumentCommand = new RelayCommand(() => DeleteDocument(), () => CanDeleteDocument())); }
}
public RelayCommand AddDocumentCommand
{
get { return _addDocumentCommand ?? (_addDocumentCommand = new RelayCommand(() => AddDocument())); }
}
public RelayCommand OpenDocumentCommand
{
get { return _openDocumentCommand ?? (_openDocumentCommand = new RelayCommand(() => OpenDocument())); }
}
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (_isSelected == value)
return;
_isSelected = value;
this.RaisePropertyChanged("IsSelected");
}
}
public string Name
{
get { return _document.DocumentName; }
set
{
if (_document.DocumentName == value)
return;
_document.DocumentName = value;
this.RaisePropertyChanged("Name");
}
}
}