我正在寻找一个简洁的示例代码片段,可以执行以下操作:
从更高级别的UserControl
,我想通过 XAML 更改子UserControl
中的对象(例如Button
(的属性。
例如,假设我有一个名为Widget
的UserControl
,其中包含Button
s 的Grid
。每个Button
都有不同的背景和边框颜色。然后,我想有一个名为WidgetPanel
的UserControl
来维护Widgets
的Grid
。
对于WidgetPanel
中的每个Widget
定义,我希望能够通过XAML设置每个按钮(分别命名为button0
,button1
,button2
(属性的BorderBrush
和Background
。我还想以编程方式更改WidgetPanel.xaml.cs
中隐藏代码的事件上的这些值。
下面是每个对象的 XAML 和隐藏代码:
XAML forWidget
<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 forWidgetPanel
:
<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();
}
}
}
在Widget
类中,定义一组影响内部按钮样式的属性。例如,对于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
}
在 XAML 中Widget
:
<Button BorderBrush="{Binding Path=FirstButtonBorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"/>
在 XAML 中WidgetPanel
:
<local:Widget x:Name="firstWidget"
FirstButtonBorderBrush="Red"/>
当然,您可以从WidgetPanel
的代码隐藏中设置此属性:
firstWidget.FirstButtonBorderBrush = new SolidColorBrush(Colors.Red);
我不确定以下想法是否有效。尝试下面的代码,它可能会解决您的查询。
连接在 WidgetPanel.xaml 中的 WidgetPanel UserControl 中加载的网格的焦点事件.cs如下所示,
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.
}
我刚刚分享了我的意见。我没有检查上面的代码。愿它对你有帮助。