开发者

Can WPF Controls be re-used?

开发者 https://www.devze.com 2023-01-04 23:17 出处:网络
For example, I have created a button with a check mark drawn on it.I need to use it in a couple places in my window.

For example, I have created a button with a check mark drawn on it. I need to use it in a couple places in my window.

<Button Width="25">
    <!-- Draw a Green checkmark -开发者_如何学Go->
    <Polyline Points="2,5,6,10,13,1" Stroke="Green" StrokeThickness="4" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeEndLineCap="Round" />
</Button>

Since I only want to change the content, I'm not sure if Styles or Templates apply. Do I need to create a UserControl?

Edit: This can be accomplished with a style. But, I only want to change the content and I don't want to override the current style.


Create a UserControl with the contents of the button you created if you want to have some sort of dynamic capability to it i.e. add new properties. If you just have a button that just has the visual appearance to have a check then I'd use a style.


This is crying out for a style. Add the following Style to your Window.Resources:

<style TargetType="Button" x:Key="tick">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Polyline Points="2,5,6,10,13,1" Stroke="Green" StrokeThickness="4" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeEndLineCap="Round" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

and you can then define your button as:

<Button Width="25" Style="{StaticResource tick}" />


Create a UserControl with the contents of the button you created. The easiest option to create the user control is to use Expression Blend. Select all of our controls to whom u want to make UserControl. Right Click and Select "make into Usercontrol". Thats it. U can use this control anywhere in your project just by drag and drop.

Hope it will help u,

Saghar Ayyaz

0

精彩评论

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