![](/img/trans.png)
[英]Inline editing TextBlock in a ListBox with Data Template (WPF)
[英]WPF ListBox style with data template
我在數據綁定,數據模板和我自己的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}"**
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.