如何在 WPF 中设置组件 UserControls 的属性 XAML
How to set properties of component UserControls in WPF XAML
我正在寻找执行以下操作的示例代码的简明片段:
从更高级别 UserControl
,我想通过 [=57] 更改子 UserControl
中对象(比如 Button
)的 属性 =].
例如,假设我有一个名为 Widget
的 UserControl
,其中包含 Grid
个 Button
。每个 Button
都有不同的背景和边框颜色。然后我想要一个名为 WidgetPanel
的 UserControl
来维护 Widgets
的 Grid
。
对于 WidgetPanel
中的每个 Widget
定义,我希望能够设置每个按钮的 BorderBrush
和 Background
(名为 button0
、button1
、button2
)通过 XAML 属性。我还想以编程方式从 WidgetPanel.xaml.cs
.
中的代码中更改事件的这些值
这是每个对象的 XAML 和隐藏代码:
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>
Widget
的隐藏代码
using System.Windows.Controls;
namespace WpfApp1
{
public partial class Widget : UserControl
{
public Widget()
{
InitializeComponent();
}
}
}
XAML 对于 WidgetPanel
:
<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.xaml.cs 中 WidgetPanel UserControl 中加载的 Grid 的 Focused 事件,
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.
}
我刚刚分享了我的意见。我没有检查上面的代码。希望对你有帮助。
在 Widget
class 中定义影响内部按钮样式的属性集。例如,对于 BorderBrush
:
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
}
在 Widget
的 XAML 中:
<Button BorderBrush="{Binding Path=FirstButtonBorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"/>
在 WidgetPanel
的 XAML 中:
<local:Widget x:Name="firstWidget"
FirstButtonBorderBrush="Red"/>
当然你可以在 WidgetPanel
的代码隐藏中设置这个 属性:
firstWidget.FirstButtonBorderBrush = new SolidColorBrush(Colors.Red);
我正在寻找执行以下操作的示例代码的简明片段:
从更高级别 UserControl
,我想通过 [=57] 更改子 UserControl
中对象(比如 Button
)的 属性 =].
例如,假设我有一个名为 Widget
的 UserControl
,其中包含 Grid
个 Button
。每个 Button
都有不同的背景和边框颜色。然后我想要一个名为 WidgetPanel
的 UserControl
来维护 Widgets
的 Grid
。
对于 WidgetPanel
中的每个 Widget
定义,我希望能够设置每个按钮的 BorderBrush
和 Background
(名为 button0
、button1
、button2
)通过 XAML 属性。我还想以编程方式从 WidgetPanel.xaml.cs
.
这是每个对象的 XAML 和隐藏代码:
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>
Widget
using System.Windows.Controls;
namespace WpfApp1
{
public partial class Widget : UserControl
{
public Widget()
{
InitializeComponent();
}
}
}
XAML 对于 WidgetPanel
:
<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.xaml.cs 中 WidgetPanel UserControl 中加载的 Grid 的 Focused 事件,
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.
}
我刚刚分享了我的意见。我没有检查上面的代码。希望对你有帮助。
在 Widget
class 中定义影响内部按钮样式的属性集。例如,对于 BorderBrush
:
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
}
在 Widget
的 XAML 中:
<Button BorderBrush="{Binding Path=FirstButtonBorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"/>
在 WidgetPanel
的 XAML 中:
<local:Widget x:Name="firstWidget"
FirstButtonBorderBrush="Red"/>
当然你可以在 WidgetPanel
的代码隐藏中设置这个 属性:
firstWidget.FirstButtonBorderBrush = new SolidColorBrush(Colors.Red);