Create a default style for a custom control

A user control is currently being created (based on WPF DataGrid). What I would like to do is set the default style to datagrid. I am currently setting the Style property, which works. But my problem arises when I create a style for changing fx. background color in the main app.xaml applications. Then all my default styles are lost, and the DataGrid looks standard only with the background set installed.

I tried using OverrideMetadata for each of the grid properties, for which I want to apply the default value, but no luck. I also tried to set each property in the constructor, but because of the priority of the property, styles from the main application are never applied.

Any ideas?

Thank you in advance

+3
source share
2 answers

If you create a style without a dictionary key, it will stylize all the objects of the type you specified in the area into which you import the style dictionary (if you specify it in Window.Resources, it will have an area for this window ... if you specify it in App.xaml ... you get the image).

  <Style TargetType="{x:Type Button}">
    <Setter Property="FontFamily" Value="Times New Roman" />
    <Setter Property="FontSize" Value="30" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Background" Value="#FFCA5132" />
  </Style>

This will give them the same style.

. , . , , , "", , Person ListView, , , . WPF "" .

+4

static?

DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomType), new FrameworkPropertyMetadata(typeof(MyCustomType)));

, Style ?

, TargetType .

:

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

    //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly 
)]
+12

Source: https://habr.com/ru/post/1714481/


All Articles