【问题标题】:WPF ListBox style with data template带有数据模板的 WPF ListBox 样式
【发布时间】:2018-10-14 14:48:31
【问题描述】:

我对数据绑定、数据模板和我自己的 WPF 列表框样式有疑问。我为 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 列中设置选择。但目前它不起作用。有任何想法吗?谢谢!

【问题讨论】:

  • ListView 是一个带列的 ListBox

标签: c# wpf listbox


【解决方案1】:

这个不对,改成TemplateBinding ItemsSource

<ContentPresenter
  x:Name="NumberLineContentControl"
  **Content="{Binding}"**

【讨论】:

    猜你喜欢
    • 2012-11-30
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多