So if you put a style in a resource without giving it a key name, it will apply to all targets on the page, right?
So, how would you let it hit all targets, but then override a setting or two on just one or two targets? I would think you could use Basedon in the target, but since you didn't give the master style a key name, how would you accomplish this partial override?
>
><Style TargetType="TextBlock">
> <Setter Property="Foreground" Value="Red"/>
></Style>
>
>
>Try it without the key and without using Style= on the target <g>
>
>this also works...
>
>
><Style TargetType="wpf2:FieldLabel">
> <Setter Property="Foreground" Value="Red"/>
></Style>
>
>
>Remember there is also a BasedOn property.
>
>But don't be afraid to use style= when you need to.
>
>Also take a peek at HeaderedContentControl took me a while to discover that one, and it looks like what you are trying to do.
>
>
> <HeaderedContentControl Header="Invoice" Style="{StaticResource styleEditHCC}">
> <TextBox Style="{StaticResource styleEditItemTextBox}"
> x:Name="txtInvoice"
> Text="{Binding Path=Invoice, UpdateSourceTrigger=PropertyChanged }"
> Width="200"/>
> </HeaderedContentControl>
>
>
>Style is basically a Visual subclass. when you get into templating controls you will discover that it is incredibly powerful.
>
>BTW I started off thinking the same way you are on this and then switched my mind about it.
>
>John
>
>
>>Thanks for sharing your thoughts and comments.
>>
>>I tried it as a Resource (even before I posted), and it works (of couse). It just that this way you have to include the style on every TextBlock in the xaml (where you want it used)
>>
>>
>> <TextBlock Style="{StaticResource FieldLabels}" Text="Job No."/>
>>
>>
>>I have so far been more drawn to this syntax of a sub-classed instance:
>>
>> <wpf2:FieldLabel Text="Job No." />
>>
>>
>>With the sub-classed method, it just seems like some maintenance benefits could be there. BUT, as you know I am still a newbie, so I don't want to dig in to hard on an opinion yet. I'm still gathering ideas on structuring all this new-found power.
>>
>>
>>>A XAML file can only have one root element and you can only do this x:Class="wpf2.FieldTextBox" in that single root element. So what your proposing won't work, they need to be separate files.
>>>
>>>Subclassing is a bit different in WPF than is was in earlier languages.
>>>
>>>Here's my view on it:
>>>
>>>If it's for appearance use styles and templates in your resource dictionaries, do not use subclasses just to restyle elements in WPF. Only subclass a base class other than a user control or a window when you need to add codebehind functionality to it that you cannot easily add in XAML or with Dependency Properties.
>>>
>>>The more subclasses you add the more complex your XAML resource dictionaries become, so it's a good idea to keep it as simple as you can. Remember that something like a red or blue foreground in your sample below (i realize that was just a sample) is often based on a property of the item behind displayed. Handle that with a trigger rather than creating a different style.
>>>
>>>That said I do have subclasses, For instance I have 2 for text boxes, 1 that does VFP like text entry handling and one that does auto complete. I am however strongly considering combining them into 1 subclass as some point.
>>>
>>>Most (but not all) of my reusable subclasses do not have a XAML component to them. They are just a C# file. If I want to restyle them I do it in a style in the Resource Dictionaries.
>>>
>>>I load my resources at the app level but you could also do them at the window level.
>>>
>>>
>>> <Application.Resources>
>>> <ResourceDictionary>
>>> <ResourceDictionary.MergedDictionaries>
>>> <ResourceDictionary Source="Themes/Default.xaml" />
>>> <ResourceDictionary Source="/cdsWPFControls;component/Drawings/Contacts.xaml" />
>>> <ResourceDictionary Source="/cdsWPFControls;component/Drawings/Tools.xaml" />
>>> </ResourceDictionary.MergedDictionaries>
>>> </ResourceDictionary>
>>> </Application.Resources>
>>>
>>>
>>>HTH
>>>
>>>John
>>>
>>>
>>>>I want to subclass certain base controls before using them on my forms so I can have one central place to adjust the visual appreance of them.
>>>>
>>>>I have this working in a file called MyControls.xaml, but I cannot figure out how to add a second class in this file.
>>>>
>>>>Here it is with just one sub-classed control, a TextBlock:
>>>>
>>>>
>>>><TextBlock x:Class="wpf2.FieldLabel"
>>>> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>>>> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>>>> Foreground="Red">
>>>></TextBlock>
>>>>
>>>>
>>>>
>>>>
>>>>But, when I add a second class into the file (for a TextBox), it gives me an error, saying 'There are multiple root elements. Line 9, position 6.' XML is not valid.
>>>>
>>>>
>>>><TextBlock x:Class="wpf2.FieldLabel"
>>>> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>>>> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>>>> Foreground="Red">
>>>></TextBlock>
>>>>
>>>>
>>>><TextBox x:Class="wpf2.FieldTextBox"
>>>> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>>>> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>>>> Foreground="Blue">
>>>></TextBox>
>>>>
>>>>
>>>>
>>>>So, how can I have multiple sub-classes in a file? Do I have to create one file for each sub-classed control?