开发者

WPF Style Reuse

I am relatively new to the WPF scene, and am having a problem understanding how styles are reused amongst controls.

My example situation is this, I'm making a control that needs a Toggle button. I want this ToggleButton to look like 开发者_如何转开发the 'expand' Button on a TreeViewItem. How would apply the TreeViewItem's button style to my own button?

From my searching I have a feeling that it isn't possible without copying XAML, but I can't justify to myself why anyone would make a UI framework that limited.

Thanks in advance.


I'm not sure there's a way of retrieving the default style/template from something and applying it to another control in XAML, although you may be able to to it in code. Although that would pose the problem that you just want to get the button part of the template and it's easier (not to mention cleaner) just to write a new style rather than hacking around to get just that part of the template.

The problem with restyling buttons is that when pressed they will go back to their default pressed appearance, same for when they are hovered. What you want to do is change the ControlTemplate of the button.

When I was starting out in WPF I found this tutorial to be quite a useful introduction to the process.

I'd recommend getting a copy of ShowMeTheTemplate to give you access to most of the default templates for controls as that will save a lot of the basic work and give you an insight into how the controls work.

When you've created your control template (or any style/template for that matter), you can store it in a resource dictionary and apply it to controls by referencing it from the relevant property using the StaticResource markup extension.

Example:

(In a resource dictionary, for example App.Resources):

<Style x:Key="myStyle" TargetType="Button">
    <Setter Property="Width" Value="70" />
</Style>

Used in a button:

<Button Style="{StaticResource myStyle}" />

Hope this helps get you started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜