【问题标题】:Excluding margin from ListBox item selection从 ListBox 项目选择中排除边距
【发布时间】:2014-04-15 05:18:03
【问题描述】:

我有一个ListBox...

<ListBox Margin="10" ItemsSource="{Binding Employees}" 
         ItemTemplate="{DynamicResource EmployeesTemplate}"
         HorizontalContentAlignment="Stretch" BorderThickness="0" 
         ScrollViewer.CanContentScroll="False"/>

...具有自定义 DataTemplate:

<Window.Resources>
    <DataTemplate x:Key="EmployeesTemplate">
        <Border BorderThickness="1" BorderBrush="Black" SnapsToDevicePixels="True"
                DockPanel.Dock="Top" Margin="0,0,0,5" Padding="5">
            <StackPanel>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="150"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <StackPanel Grid.Column="0" Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=FirstName}" Padding="0,0,5,0"/>
                        <TextBlock Text="{Binding Path=LastName}"/>
                    </StackPanel>
                    <TextBlock Grid.Column="1" Text="{Binding Path=Title}"/>
                </Grid>
                <StackPanel>
                    <TextBlock Text="{Binding Path=DateOfBirth, StringFormat={}{0:MM/dd/yyyy}}"/>
                    <TextBlock Text="{Binding Path=Address}"/>
                    <TextBlock Text="{Binding Path=PhoneNumber}"/>
                    <TextBlock Text="{Binding Path=Salary, StringFormat={}{0:C}}"/>
                </StackPanel>
            </StackPanel>
        </Border>
    </DataTemplate>
</Window.Resources>

当我从 ListBox 中选择一个项目时,选择突出显示包括分配到每个项目底部的 5 点 MarginBorder in DataTemplate):

您会在左侧注意到类似的情况,突出显示只是稍微溢出。直到现在我才注意到……嗯。所以,我想将选择突出显示限制在边框区域,并且在它之外没有任何东西,并且能够保留项目之间的边距。

我将如何做到这一点?我尝试尽可能多地操纵PaddingMargin,但我无法弄清楚。也许我必须创建一个自定义ListBoxtemplate

【问题讨论】:

    标签: c# wpf listbox listboxitem


    【解决方案1】:

    我不确定是否有更简单的方法,但它对我有用。我在DataTemplate 中添加了DataTemplate.Triggers 部分...

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                             AncestorType={x:Type ListBoxItem}}, Path=IsMouseOver}" Value="True">
            <Setter Property="Background" Value="LightBlue" TargetName="EmployeesTemplateBorder"/>
        </DataTrigger>
    
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                             AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
            <Setter Property="Background" Value="RoyalBlue" TargetName="EmployeesTemplateBorder"/>
        </DataTrigger>
    </DataTemplate.Triggers>
    

    ...和ListBoxListBox.Resrouces 部分(以摆脱默认的突出显示行为):

    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
    </ListBox.Resources>
    

    显然,这对前台没有任何作用,但这不是现在的问题,因为与这个突出显示例程相比,这相对容易。

    【讨论】:

      猜你喜欢
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      相关资源
      最近更新 更多