我有数据绑定,数据模板和我自己的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 Debug输出中出现以下错误:
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列中设置选择。但目前它不起作用。有任何想法吗?谢谢!
答案 0 :(得分:0)
这是错误的,改为TemplateBinding ItemsSource
<ContentPresenter
x:Name="NumberLineContentControl"
**Content="{Binding}"**