0

私は次のコードを持っています: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>

コード:

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

私がやろうとしているのは、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>

しかし、私はこれにアクセスできません、私は何をする必要がありますか?

4

2 に答える 2

1

これを行う正しい方法は、次のようにユーザー コントロールで Rectangle をラップすることです。

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

次に、オブジェクト自体ではなくプロパティを変更して、参照 ( )Contentを失わないようにします。BackgroundLayout

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

そして最後に、それは動作します:

<my:userControlTest>
    <my:userControlTest.IBackground>
        <Background ...>
    </my:userControlTest.IBackground>
</my:userControlTest>
于 2012-05-08T14:03:26.207 に答える
0
  • XAML でコントロールのプロパティにアクセスするには、それを依存関係プロパティにする必要があります。

  • (少なくとも私が見たものから)あなたがやろうとしていることを行うためのより一般的な方法は、内側の長方形の背景を制御する「共通の」スタイルを使用することだと思います。

    編集

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

依存関係プロパティを変更すると XAML がコンパイルされますが、ユーザー コントロールで処理する必要があります。

また、それを使用すると、背景を長方形のプロパティに設定しようとしています! それは明らかにうまくいきません。プロパティが何であるかをインスタンス化する必要があります-長方形:

<my:userControlTest>
        <my:userControlTest.IBackground>
            <Rectangle  Fill="Orange"/>
        </my:userControlTest.IBackground>
    </my:userControlTest>
于 2012-05-04T15:21:37.400 に答える