Create a control in Resources and reuse it in XAML WPF

后端 未结 3 1485
遥遥无期
遥遥无期 2020-12-14 07:34

I just try to create a simple Symbol/Geometry/Control and change and reuse it in several places in the same window.

Example: a black square with a circle in the midd

相关标签:
3条回答
  • 2020-12-14 07:53

    When you define a arbitrary Control in Resources, you can use it in the future in Control which have property Content and derived from Control class. These are the followings: ContentControl, Label, ContentPresenter, etc.

    Also you must set x:Shared="False" for resource if you want to use this resource in many Controls, because x:Shared="True" by default then one Resource is common to all - in this case, the system swears on the duplicate Content. When x:Shared="False" when is created Resource for each element whenever it its request. Quote from MSDN:

    When set to false, modifies WPF resource-retrieval behavior so that requests for the attributed resource create a new instance for each request instead of sharing the same instance for all requests.

    Example:

    <Window.Resources>
        <Canvas x:Key="Ampel" x:Shared="False">
            <Rectangle Fill="Black" HorizontalAlignment="Left" Height="52" Stroke="Black" VerticalAlignment="Top" Width="50"/>
            <Ellipse x:Name="RedGreen" Fill="Red" HorizontalAlignment="Left" Height="27" Margin="11,12,0,0" Stroke="Black" VerticalAlignment="Top" Width="28" />
        </Canvas>
    </Window.Resources>
    
    <Grid>
        <ContentControl Name="MyContentControl" Content="{StaticResource Ampel}" HorizontalAlignment="Left" />        
        <Label Name="MyLabel" Content="{StaticResource Ampel}" HorizontalAlignment="Center" />
        <ContentPresenter Name="MyContentPresenter" Content="{StaticResource Ampel}" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
    

    To change the Fill of Ellipse in code-behind, you can like this:

    private void ChangeBackground_Click(object sender, RoutedEventArgs e)
    {
        var canvas = MyContentControl.Content as Canvas;
    
        if (canvas != null)
        {
            foreach (var item in canvas.Children)
            {
                if (item is Ellipse)
                {
                    ((Ellipse)item).Fill = Brushes.Green;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-14 07:56

    You can use C# code by heritage:

    public class customBottum : Buttom
    {
            int var1;
            bool var2;
    
            public customBottum()
            {
                InitializeComponent();
            }
    
            other function ...
    }
    
    0 讨论(0)
  • 2020-12-14 08:12

    canvas is not having template property thats why we are using contencontrol here.

    <Window.Resources>
        <ControlTemplate x:Key="Ampel" TargetType="ContentControl">
            <Canvas>
                <Rectangle Fill="Black" HorizontalAlignment="Left" Height="52" Stroke="Black" VerticalAlignment="Top" Width="50"/>
                <Ellipse x:Name="RedGreen" Fill="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="Left" Height="27" Margin="11,12,0,0" Stroke="Black" VerticalAlignment="Top" Width="28" RenderTransformOrigin="0.214,0.256"/>
            </Canvas>
        </ControlTemplate>
    </Window.Resources >
    
    <ContentControl  Template="{StaticResource Ampel}" Tag="Red" ></ContentControl>
    <ContentControl  Template="{StaticResource Ampel}" Tag="Green" ></ContentControl>
    <ContentControl  Template="{StaticResource Ampel}" Tag="Blue" ></ContentControl>
    

    Output

    enter image description here

    0 讨论(0)
提交回复
热议问题