locked
[WPF 4] Inherit style from base control?

    Question

  • Hello!

    I'm trying to derive from control to slightly tune it's style. The sample case:

      public class ValidationResultsView: ListView
      {
        static ValidationResultsView()
        {
          DefaultStyleKeyProperty.OverrideMetadata(
            typeof(ValidationResultsView),
            new FrameworkPropertyMetadata(typeof(ValidationResultsView)));
        }
      }

    Generic.xaml:

      <Style TargetType="ui:ValidationResultsView"
             BasedOn="{StaticResource {x:Type ListView}}">
        <Setter Property="ItemsSource"
                Value="{Binding}" />
        <Setter Property="DisplayMemberPath"
                Value="ErrorContent" />
        <Setter Property="BorderBrush"
                Value="{x:Null}" />
        <Setter Property="Background"
                Value="Transparent" />
      </Style>

    Sadly, BasedOn gives me an error: The resource "{x:Type ListView}" could not be resolved.

    As I may see, there's at least 3 workarounds.

    1. Derive from Control/UserControl and put ListView into control's template. Sadly, use-cases of the control requires to access to most properties on ListView. 

    2. Copy-paste ListView default style and related resources. Definitely not the way to go as I do not wish to bother with all 6 themes.

    3. Generic.xaml:

    <Style x:Key="{x:Static ui:ValidationUI.ValidationResultsViewStyleKey}"
             TargetType="{x:Type ListView}">
        <Setter Property="ItemsSource"
                Value="{Binding}" />
        <Setter Property="DisplayMemberPath"
                Value="ErrorContent" />
        <Setter Property="BorderBrush"
                Value="{x:Null}" />
        <Setter Property="Background"
                Value="Transparent" />
      </Style>

    Somewhere else:

    <ListView Style="{StaticResource {x:Static ui:ValidationUI.ValidationResultsViewStyleKey}}" />

    It seems to be the best one. Until I'd to add some logic:)

    Any suggestions?

    Thanks!

    • Moved by Yves.Z Thursday, December 09, 2010 3:43 AM Designer warning (From:Windows Presentation Foundation (WPF))
    Friday, December 03, 2010 2:49 AM

All replies

  • Hi, have you tried getting the default style of the ListView? Then put it in the Resources section, and give it a key so that you can use StaticResource to reference it on the BasedOn property.

    Edit: Didn't see that you already thought of that. Anyway, you could remove the call to the OverrideMetadata method so that the default style is not overridden. Then, you won't have to specify a BasedOn property.

    • Edited by Michael Detras Friday, December 03, 2010 3:16 AM Wrong answer.
    Friday, December 03, 2010 3:12 AM
  • >Anyway, you could remove the call to the OverrideMetadata method so that the default style is not overridden

    Ok, but what about the initial goal: I'm trying to derive from control to slightly tune it's style? ;)

    Friday, December 03, 2010 6:30 AM
  • Hi Sinix,

    One thing I want to confirm is this is should be a warning in WPF designer, not an error, am I right? In your case, I prefer the third workaround. Since this thread is related to WPF designer, I also recommend you post it to WPF designer forum for a better answer.

    Based on my knowledge, when you want to customize ListView you need to work with the View property which is of type ViewBase. In fact, you might also try to derive from ListBox instead, what I mean is,

    <Style TargetType="{x:Type local:CustomControl1}" BasedOn="{StaticResource {x:Type ListBox}}">
    

    Hope it helps! If you still have any doubts or questions please feel free to let me know.

     

    Best regards,

    Yves


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Wednesday, December 08, 2010 8:45 AM
  • Hi, Yves!

    Sadly,  BasedOn="{StaticResource {x:Type ListBox}}" will not work (and could not). Anyway, I'd prefer to have more general solution as the ability to tune default style should not be ListView-only;)

    Wednesday, December 08, 2010 9:20 AM
  • Hi Sinix,

    I could understand you on the intention to figure out a general solution, and your workarounds sounds reasonable too.

    Actually the styles of ListBox and ListView are almost the same, besides some slight differences on theme color. I think you may get the property derived properly with the given code snippets.

    Anyway, since you are encountering with a design-time warning caused by WPF designer, I am moving this thread into WPF Designer forum thus you may get a better answer from designer experts live there.

    Best regards,

    Yves


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, December 09, 2010 3:37 AM
  • Please, leave it here. The problem is not on WPF designer, deriving style from base seems to be unsupported feature, so it fails on build/runtime. I do not use designer on this control and it doesn't support templates|resource dictionaries. So, you just killed any chance to get answer;)

    Anyway, there's no difference to derive from list view or listbox. Same error, as said above.

    Thursday, December 09, 2010 4:49 AM