开发者

WPF UserControl exposing style?

开发者 https://www.devze.com 2023-02-11 09:49 出处:网络
How do I expose a style as a property in a WPF USER CONTROL ? I have a user control that has a textbox. Let\'s called this textbox \"mainTextBox\". Let\'s call my control \"myControl\".

How do I expose a style as a property in a WPF USER CONTROL ?

I have a user control that has a textbox. Let's called this textbox "mainTextBox". Let's call my control "myControl".

I want the user of the control to be able to do something like :

<myControl ma开发者_开发问答inTextBoxStyle={StaticResource someStyle} >

How do I do this and is it even possible ?

Regards, Sebastian


Add a dependency property MainTextBoxStyle to your UserControl that is of the type Style.
In the constructor of your UserControl create a Binding from 'MainTextBoxStyle' to the Style-property of the TextBox.

Here an example for the binding:

mainTextBox.SetBinding(TextBox.StyleProperty,new Binding("MainTextBoxStyle"){Source=this,Mode=TwoWay});
0

精彩评论

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

关注公众号