c# - 带有数据模板的 WPF ListBox 样式
问题描述
我对数据绑定、数据模板和我自己的 WPF ListBox 样式有疑问。我为 ListBox 创建了一个样式,其中包含一些列定义以获得更好的视图。看截图:
这是我的 ListView 的 XAML 代码:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Org.Vs.TailForWin.LogView"
xmlns:business="clr-namespace:Org.Vs.TailForWin.Business.Data"
>
<Style x:Key="NumberLinesStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Text" Value="{Binding Index, Mode=OneWay}" />
</Style>
<DataTemplate x:Key="NumberLinesTemplate" DataType="{x:Type business:LogEntry}">
<TextBlock
Margin="5,1"
Style="{StaticResource NumberLinesStyle}"
/>
</DataTemplate>
<Style x:Key="ItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{x:Type local:LogWindowListBox}" TargetType="{x:Type local:LogWindowListBox}">
<Setter Property="IsSynchronizedWithCurrentItem" Value="True" />
<Setter Property="ItemContainerStyle" Value="{StaticResource ItemStyle}" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:LogWindowListBox}">
<Grid Background="{TemplateBinding Background}">
<ScrollViewer
Padding="{TemplateBinding Padding}"
CanContentScroll="True"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="8" />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border
x:Name="BorderBookmark"
Grid.Column="0"
Width="20"
Background="DarkGray"
/>
<Border
x:Name="BorderLineNumber"
Grid.Column="1"
MinWidth="80"
BorderThickness="0,0,1,0"
>
<Border.BorderBrush>
<VisualBrush>
<VisualBrush.Visual>
<Rectangle
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualHeight}"
Stroke="Black"
StrokeDashArray="2 6"
StrokeThickness="0.7"
/>
</VisualBrush.Visual>
</VisualBrush>
</Border.BorderBrush>
<ContentPresenter
x:Name="NumberLineContentControl"
Content="{Binding}"
ContentTemplate="{StaticResource NumberLinesTemplate}"
/>
</Border>
<ItemsPresenter Grid.Column="3" />
</Grid>
</ScrollViewer>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
我的问题是 ContentPresenter。在 Visual Studio 调试输出中出现以下错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'Index' property not found on 'object' ''MainWindowViewModel' (HashCode=31201899)'. BindingExpression:Path=Index; DataItem='MainWindowViewModel' (HashCode=31201899); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
如何查看 ContentPresenter 的绑定,以从我的收藏中获取正确的值?在主窗口中,我的绑定看起来是这样的:
<Grid>
<logView:LogWindowListBox
ItemsSource="{Binding LogEntries}"
behaviors:ListBoxSelector.Enabled="True"
/>
</Grid>
LogEntries 是 MainWindowViewModel 中的 ObservableCollection。第二个问题是选择。我希望用户可以在 BorderLineNumber 列中设置选择。但目前它不起作用。有任何想法吗?谢谢!
解决方案
这是错误的,更改为 TemplateBinding ItemsSource
<ContentPresenter
x:Name="NumberLineContentControl"
**Content="{Binding}"**
推荐阅读
- sql - laravel 在搜索过滤器中仅显示相关数据
- c# - 线程中的代码导致标签不更新
- java - 如何填充没有空白行的xls文件?
- regex - 使用正则表达式提取不同的连字符个人姓名变体
- python - Print a message for user while long process running in Python
- tensorflow - Keras TypeError:无法腌制 _thread.RLock 对象
- fonts - RMarkdown - 使用kable或huxtable的表格中的不同字体类型,输出到pdf?
- python - Python IF 语句与 Communicate() 函数
- javascript - Node.js - 试图理解单例
- c# - 如何在 Windows 上调试 .NET Core 3 崩溃