wpf 选择列表框项目中的项目

qqrboqgw  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(113)

我做了一个可定制的日历,在其中显示一个月的列表。当我移动鼠标时,我看到鼠标经过ListBox.Item,但单击时无法进行任何选择。**编辑:我错了,只有在它的子项(天)之外单击时,'ListBox.Item'才会被选中。**我想只选择我一个月中的一天。我尝试将所有的BackGround property设置为Transparent,但它不会改变行为。我怎么能不选择列表框项,而是选择列表框项中的项呢?(天)
下面是我实际拥有的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>

字符串

pxiryf3j

pxiryf3j1#

当ListBoxItem的IsSelected属性为true和该属性的实际值时,您似乎混淆了视觉效果。当单击ListBox中的项容器(ListBoxItem)或项(ListBoxItem的内容)时,该ListBoxItem的IsSelected属性将变为true。默认情况下,此属性的更改将反映到ListBoxItem的背景画笔的更改。这样,用户注意到该项目被点击。
但是您通过覆盖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>

字符串

相关问题