在资源中创建一个控件并在 XAML WPF 中重用它 [英] Create a control in Resources and reuse it in XAML WPF

查看:59
本文介绍了在资源中创建一个控件并在 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.

然后圆圈应该在红色和绿色之间变化(类似于单灯红绿灯).用图像做它会起作用.我尝试将其作为Window资源解决,但我不明白.

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.

想法:我把它写成一个资源,在这里我试着写成一个画布:

The idea: I write it into a resource, here I try into a 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}" /> 

这不会返回编译器错误,而是在窗口中不显示任何内容.它也不适用于 WrapPanel 或其他任何东西.

This does not return a compiler-error, but shows nothing in the window. It also doesn't work with WrapPanel or anything else.

如果它可以工作,我怎么能在代码中引用它来改变圆圈的颜色.类似于 RedGreen1.RedGreen.Fill=Brushes.Green?

And if it would work, how could I refer to it in code-behing for changing the color of the circle. Something like RedGreen1.RedGreen.Fill=Brushes.Green?

我阅读了有关红绿灯的文章.是否真的需要创建一个 UserControl 或者有没有办法用 window.resources 解决它?

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?

应用程序的总体思路是拥有一个参数列表.每一个输入正确的都被标记为绿色,只有当所有参数都被标记为绿色时才能开始计算.

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.

即使我使用红色/绿色图像运行它,我也会尝试更好地理解 WPF/XAML 并学习一些东西.

And even if I get it running with red/green-images, I try to understand WPF/XAML better and learn something.

谢谢.

推荐答案

当你在Resources中定义了一个任意的Control时,你以后可以在具有属性Content<的Control中使用它/code> 并派生自 Control 类.这些是以下内容:ContentControl标签, ContentPresenter

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.

您还必须设置 x:Shared="False" 表示资源,如果你想在许多控件中使用这个资源,因为 x:Shared="True" 默认情况下,一个资源是通用的 - 在在这种情况下,系统会对重复的内容发誓.当 x:Shared="False" 每当它请求时为每个元素创建资源.引用自 MSDN:

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:

当设置为 false 时,修改 WPF 资源检索行为,以便对属性资源的请求为每个请求创建一个新实例,而不是为所有请求共享相同的实例.

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.

示例:

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

要在代码隐藏中更改Ellipse的Fill,您可以这样:

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

这篇关于在资源中创建一个控件并在 XAML WPF 中重用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆