代码之家  ›  专栏  ›  技术社区  ›  Ben

wpf组合框阻止选择更新

  •  0
  • Ben  · 技术社区  · 6 年前

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

    enter image description here

    我不想要 ... 更改选择其中一个项目的时间。

    我尝试了很多不同的解决方案-各种绑定 SelectedIndex , SelectedValue , SelectionChanged ,玩 IsEditable , IsReadonly , IsHitTestVisible 制作 实际项目,使之成为占位符文本等。

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

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


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

    <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>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Ben    6 年前

    诀窍是去除

     Content="{TemplateBinding SelectionBoxItem}"
    

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


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