Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何根据集合定义列表框的样式?_C#_Wpf_Xaml_Listbox_Itemcontainerstyle - Fatal编程技术网

C# 如何根据集合定义列表框的样式?

C# 如何根据集合定义列表框的样式?,c#,wpf,xaml,listbox,itemcontainerstyle,C#,Wpf,Xaml,Listbox,Itemcontainerstyle,我想根据我使用的集合更改列表框的样式。 在我的代码中,第一个类有一个TypeAItemViewModel的集合。该集合将有一个无法选择的项目(假定为标题),为此,我将使用ishitestvisible 但另一个类使用的NormalParameters集合没有ishitestvisible。然后,当我使用带有NormalParameter集合的视图时,它会给我一个错误,没有ishitestvisible属性 public List<NormalParameters> Items{get

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

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

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

<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>

列表框:(使用类型aitemviewmodel集合进行工作)


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

如果不清楚,请让我知道

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

您需要在参考资料中定义不可选择的样式。并在合适的条件下进行程序检查

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>

... 你的模板

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

@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>


据我所知,您希望在列表中添加1项类型AitemViewModel,而不是NormalParameters集合?否。我有2个集合。我想用1个xaml listbox.No来显示不同的样式。在另一页。我使用的列表框是usercontrol。
<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>
<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>