简体   繁体   English

在Resources中创建一个控件,并在XAML WPF中重用它

[英]Create a control in Resources and reuse it in XAML WPF

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 middle. 示例:中间有圆圈的黑色正方形。

The circle should then change between red and green (similar to a one-light stoplight). 然后圆圈应该在红色和绿色之间变化(类似于单光红绿灯)。 Doing it with images would work. 使用图像可以实现。 I try to solve it as a Window resource, but I do not unterstand. 我尝试将其解决为Window资源,但我不会解决。

The idea: I write it into a resource, here I try into a Canvas: 想法:我将它写入资源,在这里我尝试使用Canvas:

<Window.Resources>
    <Canvas x:Key="Ampel">
        <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" RenderTransformOrigin="0.214,0.256"/>
    </Canvas>
</Window.Resources>

Then I would like to place it inside a Grid or a Panel, but how do I reference it? 然后我想将它放在网格或面板中,但我该如何引用它?

<Canvas x:Name="RedGreen1" Height="50" Width="50" DataContext="{DynamicResource Ampel}" /> 

This does not return a compiler-error, but shows nothing in the window. 这不会返回编译器错误,但在窗口中不显示任何内容。 It also doesn't work with WrapPanel or anything else. 它也不适用于WrapPanel或其他任何东西。

And if it would work, how could I refer to it in code-behing for changing the color of the circle. 如果它可以工作,我怎么能在代码behing中引用它来改变圆的颜色。 Something like RedGreen1.RedGreen.Fill=Brushes.Green ? RedGreen1.RedGreen.Fill=Brushes.Green

I read the articles about stoplights. 我读了关于红绿灯的文章。 Is it really necessary to create an UserControl or is there a way to solve it with window.resources? 是否真的有必要创建一个UserControl,还是有办法用window.resources来解决它?

General idea of the application is to have a list of parameters. 应用程序的一般概念是有一个参数列表。 Each one with a correct input is marked green and computation can only be started, if all parameters are marked green. 如果所有参数都标记为绿色,则每个具有正确输入的都标记为绿色并且只能启动计算。

And even if I get it running with red/green-images, I try to understand WPF/XAML better and learn something. 即使我用红色/绿色图像运行它,我也会尝试更好地理解WPF / XAML并学习一些东西。

Thank you. 谢谢。

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. Resources定义任意控件时,可以在将来的Control中使用它,它具有属性Content并派生自Control类。 These are the followings: ContentControl , Label , ContentPresenter , etc. 这些是以下内容: ContentControlLabelContentPresenter等。

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. 如果要在许多控件中使用此资源,还必须为资源设置x:Shared="False" ,因为默认情况下x:Shared="True"然后一个资源对所有人都是通用的 - 在这种情况下,系统发誓重复的内容。 When x:Shared="False" when is created Resource for each element whenever it its request. x:Shared="False"时为每个元素创建资源。 Quote from MSDN : 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. 设置为false时 ,修改WPF资源检索行为,以便对属性资源的请求为每个请求创建新实例,而不是为所有请求共享同一实例。

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: 要在代码隐藏中更改Ellipse的Fill ,您可以这样:

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;
            }
        }
    }
}

canvas is not having template property thats why we are using contencontrol here. canvas没有模板属性,这就是为什么我们在这里使用contencontrol。

<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 产量

在此输入图像描述

You can use C# code by heritage: 你可以使用遗产的C#代码:

public class customBottum : Buttom
{
        int var1;
        bool var2;

        public customBottum()
        {
            InitializeComponent();
        }

        other function ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM