选择 ListBox 项中的项

Select Item inside ListBox items

提问人:Siegfried.V 提问时间:7/19/2023 最后编辑:Siegfried.V 更新时间:7/21/2023 访问量:67

问:

我制作了一个可自定义的日历,其中显示了月份列表。 当我移动鼠标时,我看到鼠标越过 ,但在单击时无法进行任何选择。编辑:我错了,仅当我单击其子项(天)之外时,才会选择“ListBox.Item”。我只想选择我一个月中的一天。 我尝试将所有设置为 ,但它不会改变行为。 如何选择的不是 ListBox 项,而是 ListBox 项中的项?(天数)ListBox.ItemBackGround propertyTransparent

这是我实际上拥有的 xaml :

<Window x:Class="VSTEEL.CustomizedCalendarMenu.CustomizedCalendar"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        xmlns:ctrl="clr-namespace:VSTEEL.CustomizedCalendarMenu.ViewModel"
        Title="CustomizedCalendar" Height="450" Width="800">
    <Grid>
        <Grid.Resources>
            <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ctrl="clr-namespace:VSTEEL.CustomizedCalendarMenu.ViewModel">

                <Style TargetType="{x:Type Label}" x:Key="LABEL.WEEK">
                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    <Setter Property="BorderThickness" Value="0 0 1 1"/>
                    <Setter Property="BorderBrush" Value="#DDDDDD"/>
                    <Setter Property="Background" Value="#F1F1F1"/>
                    <Setter Property="Padding" Value="0 2 0 2"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Label}">
                                <Border 
                            BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            Padding="{TemplateBinding Padding}">
                                    <TextBlock Text="{TemplateBinding Content}" 
                                   VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

                <Style TargetType="{x:Type ListBoxItem}" x:Key="LBXI.DAY">
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="BorderThickness" Value="0 0 1 1"/>
                    <Setter Property="BorderBrush" Value="#DDDDDD"/>
                    <Setter Property="Padding" Value="10"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="50"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="50"/>
                                    </Grid.ColumnDefinitions>
                                    <Border BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            Padding="{TemplateBinding Padding}">
                                        <Border.Style>
                                            <Style TargetType="{x:Type Border}">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IsWorkingDay}" Value="True">
                                                        <Setter Property="Background" Value="#FFFFFF"/>
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding IsWorkingDay}" Value="False">
                                                        <Setter Property="Background" Value="#FF0000"/>
                                                        <Setter Property="Opacity" Value="0.5"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Border.Style>

                                    </Border>
                                    <TextBlock Text="{Binding Day}" Opacity="1" FontSize="12" Margin="2,2,0,0" FontWeight="DemiBold"/>
                                </Grid>
                                
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <Style TargetType="{x:Type ListBox}" x:Key="monthTemplate">
                    <Setter Property="AlternationCount" Value="2"/>
                    <Setter Property="ItemContainerStyle" Value="{StaticResource LBXI.DAY}"/>
                    <Setter Property="Margin" Value="10"/>
                    <Setter Property="BorderThickness" Value="1 1 0 0"/>
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="BorderBrush" Value="#DDDDDD"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBox}">
                                <Border BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <UniformGrid Columns="7">
                                            <Label Style="{StaticResource LABEL.WEEK}" Content="MON" Background="#FFFFEAEA"/>
                                            <Label Style="{StaticResource LABEL.WEEK}" Content="TUE" Background="#FFE8F9FF"/>
                                            <Label Style="{StaticResource LABEL.WEEK}" Content="WED" Background="#FFE1F1C5"/>
                                            <Label Style="{StaticResource LABEL.WEEK}" Content="THU" Background="#FFFFD7D7"/>
                                            <Label Style="{StaticResource LABEL.WEEK}" Content="FRI" Background="#FFE9F9E4"/>
                                            <Label Style="{StaticResource LABEL.WEEK}" Content="SAT" Background="#FFF7F6E3"/>
                                            <Label Style="{StaticResource LABEL.WEEK}" Content="SUN" Background="#FFC4DAE4"/>
                                        </UniformGrid>

                                        <ItemsPresenter Grid.Row="1"/>
                                    </Grid>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="ItemsPanel">
                        <Setter.Value>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="7"/>
                            </ItemsPanelTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <Style TargetType="{x:Type ListBox}" x:Key="yearTemplate">
                    <Setter Property="AlternationCount" Value="2"/>
                    <Setter Property="Margin" Value="10"/>
                    <Setter Property="BorderThickness" Value="1 1 0 0"/>
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="BorderBrush" Value="#DDDDDD"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBox}">
                                <Border BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <UniformGrid Columns="3"/>
                                        <ListBox ItemsSource="{Binding ListDays}" Style="{StaticResource monthTemplate}"/>
                                    </Grid>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="ItemsPanel">
                        <Setter.Value>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="3"/>
                            </ItemsPanelTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                
            </ResourceDictionary>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Content="Preview" Click="nextMonth_Click" Margin="4" Padding="4"/>
            <Button Content="Next" Click="previousMonth_Click" Margin="4" Padding="4"/>
        </StackPanel>
        <ListBox Grid.Row="1" ItemsSource="{Binding ListMonths}">
            <ListBox.Style>
                <Style TargetType="{x:Type ListBox}">
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="ItemsPanel">
                        <Setter.Value>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="3"/>
                            </ItemsPanelTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.Style>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <ListBox Grid.Row="1" ItemsSource="{Binding ListDays}" Style="{StaticResource monthTemplate}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
</Window>
C# WPF 模板 列表框

评论

0赞 emoacht 7/20/2023
您尚未定义对应于 LBXI 中的 IsSelected 属性值的可视元素。DAY,因此无法查看它是否实际被选中。
0赞 Siegfried.V 7/20/2023
@emoacht如何设置属性?在模板中,没有一个元素有这样的属性?IsSelected
0赞 Siegfried.V 7/20/2023
@emoacht 然后,我不知道你是否理解良好:问题不在于收回选定的项目值,而是我无法单击它们,就好像在主网格上处于活动状态,而不是在子元素上一样。我想做的是像 ListView 一样,并选择一个日期列表。Over
0赞 emoacht 7/20/2023
1)我的意思是ListBoxItem.IsSelected属性。通常,如果单击 ListBox 中的某个项,则该项的 IsSelected 属性将变为 true。2) 这仅仅是因为在更改项的 IsSelected 属性时,您尚未定义视觉交互。因此,无论 IsSelected 属性的当前值如何,您都看不到更改。

答:

1赞 emoacht 7/20/2023 #1

当 ListBoxItem 的 IsSelected 属性为 true 和该属性的实际值时,您似乎混淆了视觉效果。单击 ListBox 中的项容器 (ListBoxItem) 或项(ListBoxItem 的内容)时,该 ListBoxItem 的 IsSelected 属性将变为 true。默认情况下,此属性的更改将反映到 ListBoxItem 的 Background Brush 的更改中。因此,用户会注意到该项目被单击。

但是,您通过重写 ListBoxItem 的 Style 有效地扼杀了这种视觉效果。要解决此问题,只需为您的样式添加替代视觉效果即可。

<Style TargetType="{x:Type ListBoxItem}" x:Key="LBXI.DAY">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0 0 1 1"/>
    <Setter Property="BorderBrush" Value="#DDDDDD"/>
    <Setter Property="Padding" Value="10"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Grid>
                    ...
                    <Border BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            Padding="{TemplateBinding Padding}">
                            ...
                    </Border>
                    <TextBlock Text="{Binding Day}" Opacity="1" FontSize="12" Margin="2,2,0,0" FontWeight="DemiBold"/>
                </Grid>
                <!-- Sample visual effect when IsSelected is true -->
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="BorderThickness" Value="5"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

评论

0赞 Siegfried.V 7/21/2023
如果我理解得好,选择是有效的,但我只是没有看到它,因为我做了“我自己的风格”?因为我也尝试添加(我添加了该属性),但绑定不起作用。<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
0赞 Siegfried.V 7/21/2023
现在绑定工作正常。所以我看到 2 个选项:1) 我上次在绑定方面做错了什么。2)这是你的伎俩,有帮助。无论如何,谢谢你,它帮了大忙。