83200

WPF combobox prevent selection update

Question:

I've got a ComboBox that looks like this:

<a href="https://i.stack.imgur.com/hB0VR.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/hB0VR.png" data-original="https://i.stack.imgur.com/hB0VR.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

I don't want the value of ... to change when one of the items is selected.

I've tried a ton of different solutions - various bindings on SelectedIndex, SelectedValue, SelectionChanged, playing with IsEditable, IsReadonly, IsHitTestVisible, making ... an actual item, making it placeholder text, etc, etc, etc.

Every time I select an item, the ... updates with the child value. I want it to stay the same.

How can I prevent the combobox from automatically updating the text on selection, but still have it able to select a choice?

<hr />

If it helps, here's the custom template for that image:

<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 its corresponding 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>

Answer1:

The trick was to remove

Content="{TemplateBinding SelectionBoxItem}"

from the template (both in general and also the code posted in the question).

<hr />

Thanks to @shadow32's answer to my separate question <a href="https://stackoverflow.com/a/50805408/385273" rel="nofollow">https://stackoverflow.com/a/50805408/385273</a>.

Recommend

  • WPF Popup IsOpen issue
  • WPF combobox disabled background color
  • Image Map-like Blackberry Control - CLDC Application
  • Handling exclusive-or conditions when binding to two boolean properties in XAML
  • keyup event in wpf listbox datatemplate
  • android: Unable to pause activity : java.lang.IllegalArgumentException: Service not registered:
  • Android ListView with toggle button
  • Toggle button in a list view loose their state when scrolled of screen in Android
  • How to notify parent's view model when child's view model property is changed?
  • Could not re-enable the KeyGaurd once disabled it
  • How to automatically pop-up keyboard?
  • Toolbar shifts up, when soft keyboard appears
  • How to start server for Selenium grid Java Maven setup
  • Allocating a 2D contiguous array within a function
  • Visual studio 2015 keystroke with mouse button
  • Android Lock Screen C# .NET Replica
  • Dynamically load css stylesheet and wait for it to load
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • WPF Template Binding in ToggleButton UserControl
  • Trying to get the char code of ENTER key
  • Force show.bind execution
  • Get one-time binding to work for ng-if
  • Why value captured by reference in lambda is broken? [duplicate]
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • How to model a transition system with SPIN
  • Symfony2: How to get request parameter
  • QuartzCore.framework for Mono Develop
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • Cant find why the layout is getting smaller
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize