首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >WPF组合框阻止选择更新

WPF组合框阻止选择更新
EN

Stack Overflow用户
提问于 2018-06-12 00:22:40
回答 1查看 115关注 0票数 0

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

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

我尝试过很多不同的解决方案--在SelectedIndexSelectedValueSelectionChanged上进行各种绑定,使用IsEditableIsReadonlyIsHitTestVisible,让...成为一个实际的项目,让它成为占位符文本,等等等等。

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

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

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

代码语言:javascript
复制
<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>

...and它对应的XAML:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-12 04:32:04

诀窍就是移除

代码语言:javascript
复制
 Content="{TemplateBinding SelectionBoxItem}"

来自模板(包括一般代码和问题中发布的代码)。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50802165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档