Narendra
Narendra

Reputation: 3117

Apply certain stuff to multiple combobox

I am not sure if apply style would be a better title so I made it apply stuff. Below is certain piece of xaml which I use to remove the default arrow from ComboBox in my WPF application.

<ComboBox ItemsSource="{Binding Path=ConnTypes,Mode=OneTime, 
    UpdateSourceTrigger=PropertyChanged}" SelectedIndex="0">
    <ComboBox.Resources>
        <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">0</sys:Double>
    </ComboBox.Resources>
</ComboBox>

Now I want to do the same thing to all the ComboBox without replicating the same piece of <ComboBox.Resources> for all the ComboBox . I want to define this(<ComboBox.Resources>) only once and this should be applied to all the ComboBox in the window.

Can you please tell me a way to do this?

Thanks in advance.

Upvotes: 0

Views: 89

Answers (1)

Viv
Viv

Reputation: 17380

Add it in your Window.Resources via a Style without a x:key to thereby apply to all ComboBox control's in the Window implicitly

<Window.Resources>
  <Style TargetType="{x:Type ComboBox}">
    <Style.Resources>
      <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">0</sys:Double>
    </Style.Resources>
  </Style>
</Window.Resources>

Sidenote:

Do remember that your current approach "works" to hide the arrow of the ComboBox, but it does a bit more than that. So be aware of it incase you aren't already.

Setting the SystemParameters.VerticalScrollBarWidthKey to 0 will thereby "hide" the vertical scrollbar in the ComboBox's Popup as well which uses the same system parameter for it's Width. So if that behavior is not desired, you're better off editing the ComboBox template and just set the "arrow" Path to have a Transparent Fill or just remove it all-together.

Upvotes: 2

Related Questions