WPF XAMLでコンポーネントのUserControlのプロパティを設定する方法:より高いレベルUserControl
から私は次のことを行いサンプルコードの簡潔なスニペットを探しています
、私は言う(オブジェクトのプロパティを変更したいですa Button
)をXAML経由でサブUserControl
内に追加します。
たとえば、私はWidget
と呼ばれるUserControl
があり、Grid
がButton
であるとします。各Button
には、背景と境界線の色が異なります。私はのWidgets
を維持するWidgetPanel
と呼ばれるUserControl
を持っていたいと思います。 WidgetPanel
内の各Widget
定義について
、IはBorderBrush
とXAMLを介してプロパティ(button0
、それぞれbutton1
、button2
命名)各個々のボタンの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();
}
}
}
'WidgetPanel'とそれらにバインドされた' Widget'にいくつかのプロパティを定義します。 –
私が与えた例からそのようなコードで答えを出すことができれば、私は最も感謝しています。 –
ウィジェット内のボタンのリストはあらかじめ定義されていますか?もしそうなら、 'Widget'クラス(' FirstButtonBorderBrush'や 'FirstButtonBackground'のような)で所望の依存性プロパティを作成し、@LeiYangが言ったようにボタンのプロパティをバインドすることができます。 – Maxim