首页 > 解决方案 > WPF 组合框阻止选择更新

问题描述

我有一个ComboBox看起来像这样的:

在此处输入图像描述

我不希望在...选择其中一项时改变 的值。

我已经尝试了很多不同的解决方案 - , , 上的各种绑定,SelectedIndex玩, , ,制作一个实际的项目, 使它成为占位符文本等, 等等。SelectedValueSelectionChangedIsEditableIsReadonlyIsHitTestVisible...

每次我选择一个项目时,都会...使用子值进行更新。我希望它保持不变。

如何防止组合框自动更新选择的文本,但仍然可以选择一个选项?


如果有帮助,这是该图像的自定义模板:

<ResourceDictionary
    x:Class="ComboBoxA"
    xmlns:local="clr-namespace:MyTemplates"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ControlTemplate x:Key="ComboBoxA" TargetType="{x:Type ComboBox}">
        <Grid>
            <ToggleButton 
                ClickMode="Press" 
                Focusable="false"
                IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                Name="ToggleButton" 
            >
                <ToggleButton.Template>
                    <ControlTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition Width="36" />
                            </Grid.ColumnDefinitions>

                            <Border
                                x:Name="Border" 
                                Grid.ColumnSpan="2"
                                CornerRadius="0"
                                BorderThickness="1" />
                            <Border 
                                Grid.Column="0"
                                CornerRadius="0" 
                                Margin="1" 
                                Background="Transparent" 
                                BorderThickness="0"
                            />
                            <Path 
                                x:Name="Arrow"
                                Grid.Column="1"     
                                Fill="#707070"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Visibility="Collapsed"
                                Data="M0,0 L8,0 L4,4 z"
                            />
                            <TextBlock 
                                Margin="4,6" 
                                Foreground="#282828" 
                                Grid.Column="0" 
                                Text="{Binding Path=(local:ComboBoxAHelper.Placeholder), RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" 
                            />
                        </Grid>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="Arrow" Property="Visibility" Value="Visible"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="#d9d9d9"/>
                            </Trigger>

                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter TargetName="Arrow" Property="Visibility" Value="Visible"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="#d9d9d9"/>
                            </Trigger>

                            <DataTrigger Binding="{Binding Path=(local:ComboBoxAHelper.ShowBorders), RelativeSource={RelativeSource TemplatedParent}}" Value="True">
                                <Setter TargetName="Arrow" Property="Visibility" Value="Visible"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="#d9d9d9"/>
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </ToggleButton.Template>
            </ToggleButton>

            <ContentPresenter 
                Content="{TemplateBinding SelectionBoxItem}"
                ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                HorizontalAlignment="Left" 
                IsHitTestVisible="False"  
                Margin="3,3,23,3"
                Name="ContentSite" 
                VerticalAlignment="Center"
            />

            <Popup 
                AllowsTransparency="True" 
                Focusable="False"
                IsOpen="{TemplateBinding IsDropDownOpen}"
                Name="Popup"
                PopupAnimation="Slide"
             >
                <Grid Name="DropDown"
                    MaxHeight="{TemplateBinding MaxDropDownHeight}"
                    MinWidth="{TemplateBinding ActualWidth}"
                    SnapsToDevicePixels="True"                
                >
                    <Border 
                        Background="White"
                        BorderBrush="#d9d9d9"
                        BorderThickness="1"
                        x:Name="DropDownBorder"
                    />
                    <ScrollViewer Margin="4,6" SnapsToDevicePixels="True">
                        <StackPanel 
                            IsItemsHost="True" 
                            KeyboardNavigation.DirectionalNavigation="Contained" 
                        />
                    </ScrollViewer>
                </Grid>

                <Popup.Style>
                    <Style TargetType="Popup">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=(local:ComboBoxAHelper.RightAlignPopup), RelativeSource={RelativeSource TemplatedParent}}" Value="True">
                                <Setter Property="Placement" Value="Left" />
                                <Setter Property="VerticalOffset" Value="{Binding ActualHeight, RelativeSource={RelativeSource TemplatedParent}}" />
                                <Setter Property="HorizontalOffset" Value="{Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Popup.Style>
            </Popup>
        </Grid>
    </ControlTemplate>

    <Style x:Key="ComboBoxAItem" TargetType="{x:Type TextBlock}">
        <Setter Property="FontSize" Value="12" />
        <Setter Property="Foreground" Value="#282828" />
        <Setter Property="Padding" Value="4" />
    </Style>
</ResourceDictionary>

...及其相应的 XAML:

<ComboBox 
    templates:ComboBoxAHelper.Placeholder="..."
    templates:ComboBoxAHelper.RightAlignPopup="True"
    templates:ComboBoxAHelper.ShowBorders="True"
    HorizontalAlignment="Right" 
    IsReadOnly="True"
    IsEditable="False"
    SelectedValue="x:Null"
    Template="{StaticResource ComboBoxA}" 
>
    <ComboBoxItem>
        <TextBlock Style="{StaticResource ComboBoxAItem}">Close</TextBlock>
    </ComboBoxItem>
    <ComboBoxItem>
        <TextBlock Style="{StaticResource ComboBoxAItem}">Delete</TextBlock>
    </ComboBoxItem>
</ComboBox>

标签: c#.netwpfxaml

解决方案


诀窍是删除

 Content="{TemplateBinding SelectionBoxItem}"

来自模板(一般情况下以及问题中发布的代码)。


感谢 @shadow32 对我的单独问题https://stackoverflow.com/a/50805408/385273的回答。


推荐阅读