0

我想根据我使用的集合更改列表框的样式。在我的代码中,第一个类有一个TypeAItemViewModel. 该集合将有 1 个无法选择的项目(假设为标题),为此我将使用IsHitTestVisible.

但是另一个类使用NormalParameter没有 s的集合IsHitTestVisible。然后,当我将视图与NormalParameter集合一起使用时,它会给我一个没有IsHitTestVisible属性的错误。

public List<NormalParameters> Items{get;set;}
public List<TypeAItemViewModel> Items;

类: TypeAItemViewModel

public class TypeAItemViewModel
{
    private TypeAParameter _parameter;      
    public bool IsHitTestVisible{get;set;}
}

风格:

<Style x:Key="SelectableListBoxItem" TargetType="{x:Type ListBoxItem}">
    <Setter Property="IsHitTestVisible" Value="{Binding IsHitTestVisible}" />   
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsHitTestVisible}" Value="false">
            <Setter Property="FontWeight" Value="SemiBold" />
            <Setter Property="FontSize" Value="{StaticResource FontSizeTextBlock}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

列表框:(使用 TypeAItemViewModel 的集合可以很好地工作)

<ListBox HorizontalContentAlignment="Stretch" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" ItemContainerStyle="{DynamicResource SelectableListBoxItem}">
    <ListBox.ItemTemplate>                    
        <DataTemplate>
            <Grid Height="{StaticResource Height}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="100" />
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="0">
                    <TextBlock DataContext="{Binding Name}" Text="{Binding Value}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                </Grid>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我认为它应该与 ItemContainerStyle 一起使用,但我仍然无法解决它。

如果不清楚,请告诉我。

编辑:我有 2 个收藏。我想用 1 xaml 列表框显示它们不同的样式。 编辑:我使用的列表框是用户控件

4

2 回答 2

1

您需要在资源中定义不可选择的样式。并以编程方式在合适的条件下对其进行检查。

MyListBox.ItemContainerStyle = (Style) MyListBox.Resources["SelectableListBoxItem"];
or
MyListBox.ItemContainerStyle = null;

你的 XAML

<ListBox x:Name="MyListBox" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" ItemContainerStyle="{DynamicResource SelectableListBoxItem}">
<ListBox.Resources>
<Style x:Key="SelectableListBoxItem" TargetType="{x:Type ListBoxItem}">
    <Setter Property="IsHitTestVisible" Value="{Binding IsHitTestVisible}" />   
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsHitTestVisible}" Value="false">
            <Setter Property="FontWeight" Value="SemiBold" />
            <Setter Property="FontSize" Value="{StaticResource FontSizeTextBlock}" />
        </DataTrigger>
        <Trigger Property="IsSelected" Value="True">
             <Setter Property="Background" Value="{x:Null}" />
             <Setter Property="BorderBrush" Value="{x:Null}" />
        </Trigger>
    </Style.Triggers>
    </Style>
</ListBox.Resources>

    <ListBox.ItemTemplate>                    
... your template
    </ListBox.ItemTemplate>
</ListBox>

这不是最终的解决方案,但值得深思:)

于 2014-01-21T10:26:14.253 回答
0

@Valera 非常感谢。你的回答对我的问题来说是一种非常健康的食物。

我添加了一个新的布尔属性,然后使用它来定义我想要使用的样式。

<Style x:Key="ListBoxWithIsHitTestVisible" TargetType="{x:Type ListBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsListBoxWithHeaderItem}" Value="true">
            <Setter Property="ItemContainerStyle" Value="{DynamicResource SelectableListBoxItem}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

<Style x:Key="SelectableListBoxItem" TargetType="{x:Type ListBoxItem}">
    <Setter Property="IsHitTestVisible" Value="{Binding IsHitTestVisible}" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsHitTestVisible}" Value="false">
            <Setter Property="FontWeight" Value="SemiBold" />
            <Setter Property="FontSize" Value="{StaticResource FontSizeTextBlock}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

这是一个列表框 xaml 代码

<ListBox Grid.Row="1" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" Style="{StaticResource ListBoxWithIsHitTestVisible}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Height="{StaticResource Height}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="100" />
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="0">
                    <TextBlock DataContext="{Binding Name}" Text="{Binding Value}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                </Grid>
            </Grid>
        </DataTemplate>                    
    </ListBox.ItemTemplate>
</ListBox>
于 2014-01-21T11:03:51.220 回答