2017-07-03 6 views
-1

WPF XAMLでコンポーネントのUserControlのプロパティを設定する方法:より高いレベルUserControlから私は次のことを行いサンプルコードの簡潔なスニペットを探しています

、私は言う(オブジェクトのプロパティを変更したいですa Button)をXAML経由でサブUserControl内に追加します。

たとえば、私はWidgetと呼ばれるUserControlがあり、GridButtonであるとします。各Buttonには、背景と境界線の色が異なります。私はのWidgetsを維持するWidgetPanelと呼ばれるUserControlを持っていたいと思います。 WidgetPanel内の各Widget定義について

、IはBorderBrushとXAMLを介してプロパティ(button0、それぞれbutton1button2命名)各個々のボタンのBackground設定できるようにしたいです。 WidgetPanel.xaml.csのコードのイベントからこれらの値をプログラムで変更したいと思います。ここ

はXAMLおよび各オブジェクトの背後にあるコードである:Widget

<UserControl x:Class="WpfApp1.Widget" 
      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" 
      xmlns:local="clr-namespace:WpfApp1" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Button BorderBrush="Black" BorderThickness="4" Background="#FF249AA6" Grid.Row="0"/> 
     <Button BorderBrush="Blue" BorderThickness="4" Background="#FFFF0046" Grid.Row="1"/> 
     <Button BorderBrush="Orange" BorderThickness="4" Background="Blue" Grid.Row="2"/> 
    </Grid> 
</UserControl> 

コード後ろため

XAML Widget

using System.Windows.Controls; 

namespace WpfApp1 
{ 
    public partial class Widget : UserControl 
    { 
     public Widget() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

ためWidgetPanelためXAML:

<UserControl x:Class="WpfApp1.WidgetPanel" 
      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" 
      xmlns:local="clr-namespace:WpfApp1" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <local:Widget Grid.Column="0"/> 
     <local:Widget Grid.Column="1"/> 
    </Grid> 
</UserControl> 

WidgetPanelのための背後にあるコード:

using System.Windows.Controls; 

namespace WpfApp1 
{ 
    public partial class WidgetPanel : UserControl 
    { 
     public WidgetPanel() 
     { 
      InitializeComponent(); 
     } 
    } 
}  
+0

'WidgetPanel'とそれらにバインドされた' Widget'にいくつかのプロパティを定義します。 –

+0

私が与えた例からそのようなコードで答えを出すことができれば、私は最も感謝しています。 –

+0

ウィジェット内のボタンのリストはあらかじめ定義されていますか?もしそうなら、 'Widget'クラス(' FirstButtonBorderBrush'や 'FirstButtonBackground'のような)で所望の依存性プロパティを作成し、@LeiYangが言ったようにボタンのプロパティをバインドすることができます。 – Maxim

答えて

1

Widgetクラスでは、内部ボタンのスタイルに影響を与える一連のプロパティを定義します。たとえば、BorderBrush用:WidgetのXAMLで

public partial class Widget : UserControl 
{ 
    public Widget() 
    { 
     InitializeComponent(); 
    } 

    // BorderBrush for first button 

    public static readonly DependencyProperty FirstButtonBorderBrushProperty = 
     DependencyProperty.Register("FirstButtonBorderBrush", 
            typeof(Brush), 
            typeof(Widget)); 

    public Brush FirstButtonBorderBrush 
    { 
     get { return (Brush)GetValue(FirstButtonBorderBrushProperty); } 
     set { SetValue(FirstButtonBorderBrushProperty, value); } 
    } 

    // ... repeat for other buttons 
} 

WidgetPanelのXAMLで

<Button BorderBrush="{Binding Path=FirstButtonBorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"/> 

:あなたはのコードビハインドからこのプロパティを設定することができますもちろん

<local:Widget x:Name="firstWidget" 
       FirstButtonBorderBrush="Red"/> 

WidgetPanel

firstWidget.FirstButtonBorderBrush = new SolidColorBrush(Colors.Red); 
+0

私は今日これを試していただきますありがとうございます。それがうまくいくならば、受け入れてください。しかし、そのような単純なことのための複雑な構文は正しいのですか? –

+0

あなたはそれに慣れるでしょう:) – Maxim

+0

いいえ。私はそれを得たと思う。私たちがやっていることは、実際のオブジェクトのget/setを設定することではなく、私が変更しているButton.Backgroundです。代わりに、私たちがやっているように見えるのは、ウィジェットのXAMLキーワード "Background"をBrushオブジェクトのセッターにリダイレクトすることです。それは正しいと思いますか? Phew。 WPFについての包括的なチュートリアルがあったといいですね...クールだし、トリックを知っているとワークフローは素晴らしいですが、時には単純なものに対する答えを見つけるのは難しいです。助けてくれてありがとう@Maxim –

0

私は下のアイデアの仕事かどうかを確認していません。以下のコードを試してみてください。グリッドの

ワイヤー集束イベントは、以下のようにWidgetPanel.xaml.csにWidgetPanelのUserControl内

grid.Focused += Grid_Focused; //where grid is the name of Grid loaded inside a WidgetPanel user control. 

private void Grid_Focused(object sender, EventArgs e) 
{ 
    Grid grid = sender as Grid; 
    //here you can get the children of grid i.e, you can get the usercontrols in Widget.xaml 
    //From the user control get its children. So you can easily get the buttons here and change the respective properties. 
} 

をロードし、私はちょうど私の意見を共有しています。私は上記のコードをチェックしていません。それがあなたを助けてくれますか?

+0

これは私の質問に対する答えではありませんが、後で有益な情報になるかもしれません。ありがとう。 –

関連する問題