Laden...

Stylig eines CustomControls auf ComboBox Basis

Letzter Beitrag vor 9 Tagen 3 Posts 195 Views
Stylig eines CustomControls auf ComboBox Basis

Hallo Community,

ich versuche mich nach Jahren wieder in WPF reinzuarbeiten, habe aber das Gefühl alles vergessen zu haben. Ich versuche ein CustomControl von einer ComboBox zu bauen das neben der ComboBox einen ResetButton anzeigen zu hat.

 public class ResetableComboBox : ComboBox
    {
        // STATIC PROPERTIES
        public static readonly RoutedEvent ResetEvent;

        // STATIC CONSTRUCTORS
        static ResetableComboBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ResetableComboBox), new FrameworkPropertyMetadata(typeof(ResetableComboBox)));

            ResetEvent = EventManager.RegisterRoutedEvent(nameof(Reset), RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(ResetableComboBox));
        }

        // FIELDS
        private Button _resetButton;


        // METHODS
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _resetButton = Template?.FindName("resetButton", this) as Button;

            if (_resetButton != null)
            {
                _resetButton.Click += ResetButton_Click;
            }
        }

        private void ResetButton_Click(object sender, RoutedEventArgs e) => OnReset();

        protected virtual void OnReset() => RaiseEvent(new RoutedEventArgs(ResetEvent));

        // EVENTHANDLER

        public event RoutedEventHandler Reset
        {
            add { AddHandler(ResetEvent, value); }
            remove { RemoveHandler(ResetEvent, value); }
        }
    }
    }

Der C# Code klappt auch soweit wenn ich den ResetButton drücke dann wird auch das Event geworfen.  In der Generic.xaml habe ich dann in den beiden ComboxTemplates (habe mir per VS > WPF Designer > ComboBox Selektion > Edit Template das komplette Template ausgeben lassen und an mein neues Control angepasst)

<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type customControls:ResetableComboBox}">
        <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
            </Grid.ColumnDefinitions>
            <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                <Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
                    <Border x:Name="dropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                        <ScrollViewer x:Name="DropDownScrollViewer">
                            <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
                                <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                    <Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/>
                                </Canvas>
                                <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Grid>
                        </ScrollViewer>
                    </Border>
                </Themes:SystemDropShadowChrome>
            </Popup>
            <Button x:Name="resetButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="2" Style="{StaticResource ComboBoxResetButton}" Margin="-1,0,0,0" /> 
            <ToggleButton x:Name="toggleButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
            <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Grid>

Wenn ich nun das ganze in einem Window einbinden klappt auch alles. Meine Frage wäre jetzt, müsste es nicht möglich sein auf Window Ebene ein neuen Style anzulegen für dieses Control.

<Style TargetType="{x:Type customControls:ResetableComboBox}">
            <Setter Property="Background" Value="AliceBlue"/>
        </Style>

Aber das ändert die Farbe nicht. Im WPF Buch von Huber wird im Kapitel "CustomControls" auf Window Ebene  im Style auch das Template überschrieben, ich war aber der Meinung dass das auch so gehen müsste.

Grüße

Daniel

Mit freundlichen Grüßen
lutzeslife

TheInfo

War denn das wichtig:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                                     //(used if a resource is not found in the page, 
                                     // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                              //(used if a resource is not found in the page, 
                                              // app, or any theme specific resource dictionaries)
)]

hab auch schon ewig nix mehr wirklich mit WPF gemacht

cSharp Projekte : https://github.com/jogibear9988

Das kenne ich nicht muss ich mal schauen ob das Auswirkungen hat Danke

Mit freundlichen Grüßen
lutzeslife