Search code examples
c#wpfuser-controlsstyles

convert usercontrol control to style


hi im new in wpf I have a control that has been created in user control I want to create this control in style I do not know what to do: this is xaml codes: https://gist.github.com/ghost1372/8b3db759241b3ddb838789e446efb0b4#file-multiselectcombo-xaml
and this is cs codes:
https://gist.github.com/ghost1372/8b3db759241b3ddb838789e446efb0b4#file-multiselectcombo-cs


Solution

  • Well basically all you have to do is to define a Style and set the properties using Setters. Something like:

    <Style x:Key="MultiSelectCombo" TargetType="{x:Type ComboBox}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        ...
        <Setter Property="Template">
            <Setter.Value>
                <!-- Put the control template you used in your UserControl -->
    
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <!-- Put the DataTemplate you used in your UserControl -->
    
            </Setter.Value>
        </Setter>
    </Style>
    

    Now this style can be applied to any ComboBox and will change its look. However if you want also change the behavior you need to create a MultiSelectCombo class that overrides ComboBox and implements the logic you want.