简体   繁体   English

通过XAML编辑WPF UserControl组件

[英]WPF UserControl component editing by XAML

I have the following code: XAML: 我有以下代码:XAML:

<UserControl x:Class="RBSoft.WPF.RedConsoleViewer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="355" d:DesignWidth="691" Name="ConsoleUI_Control" KeyDown="ConsoleUI_Control_KeyDown">
    <Grid Name="_Layout">
        <Rectangle Name="BackgroundLayout">
            <!--...-->
        </Rectangle>
    </Grid>
</UserControl>

Code: 码:

public Rectangle IBackground
{
    get { return this.BackgroundLayout; }
    set { this.BackgroundLayout = value; }
}

What I'm tying to do is edit the rectangle (BackgroundLayout) from the XAML editor like so: 我想做的是从XAML编辑器编辑矩形(BackgroundLayout),如下所示:

<Window x:Class="LifeEnvironment.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="1064" Width="910"
        xmlns:my="clr-namespace:RBSoft.WPF;assembly=RBSoft.WPF"
        WindowStyle="None"
        WindowState="Maximized"
        WindowStartupLocation="CenterScreen">
    <Grid>
        <my:userControlTest>
            <my:userControlTest.IBackground>
                <Background ...>
            </my:userControlTest.IBackground>
        </my:userControlTest>
    </Grid>
</Window>

But I have no access to this, what I need to do? 但是我无权访问,我该怎么办?

The correct way to do it, is by wrapping up the Rectangle with an User Control, like this: 正确的方法是使用用户控件包装矩形,如下所示:

<UserControl x:Class="RBSoft.WPF.RedConsoleViewer" ...>
    <Grid Name="_Layout">
        <UserControl Name="BackgroundLayout">
            <Rectangle .../>
        </UserControl>
    </Grid>
</UserControl>

and then, change the Content property instead the object itself, so you don't lose the reference( BackgroundLayout ): 然后,更改Content属性而不是对象本身,这样就不会丢失引用( BackgroundLayout ):

public Rectangle IBackground
{
    get { return (Rectangle)this.BackgroundLayout.Content; }
    set { this.BackgroundLayout.Content = value; }
}

and finally, it will work: 最后,它将起作用:

<my:userControlTest>
    <my:userControlTest.IBackground>
        <Background ...>
    </my:userControlTest.IBackground>
</my:userControlTest>
  • in order to have access to control's property in XAML you need to make it a Dependency Property. 为了能够访问XAML中控件的属性,您需要使其成为依赖项属性。

  • i think (at least from what I have seen) a more common way to do what you're trying to do is to use a "common" style that controls the inner rectangle's background. 我认为(至少从我所看到的情况来看)一种更通用的方式来执行您要尝试的操作,是使用一种“通用”样式来控制内部矩形的背景。

    EDIT 编辑

     public static readonly DependencyProperty IBackgroundProperty = DependencyProperty.Register("IBackground", typeof(Rectangle), typeof(NameOfYourUserControl)); public Rectangle IBackground { get { return (Rectangle)GetValue(IBackgroundProperty); } set { SetValue(IBackgroundProperty, value); } } 

changing to dependency property will make your XAML compile, however, you still have to process it in your user control. 更改为依赖项属性将使您的XAML可以编译,但是,您仍然必须在用户控件中对其进行处理。

Also when you use it, you are trying to set the background to a property of type rectangle! 同样,在使用它时,您尝试将背景设置为矩形类型的属性! That clearly won't work. 那显然是行不通的。 You must instantiate what your property is - a rectangle: 您必须实例化属性-一个矩形:

<my:userControlTest>
        <my:userControlTest.IBackground>
            <Rectangle  Fill="Orange"/>
        </my:userControlTest.IBackground>
    </my:userControlTest>

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

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