/ / Como definir propriedades dos componentes UserControls no WPF XAML - c #, wpf, xaml, user-controls

Como definir propriedades do componente UserControls no WPF XAML - c #, wpf, xaml, controles de usuário

Estou procurando um trecho conciso de código de exemplo que faça o seguinte:

De um nível superior UserControl, Gostaria de alterar a propriedade de um objeto (digamos Button) dentro de um sub UserControl via XAML.

Por exemplo, digamos que eu tenho um UserControl chamado Widget que contém um Grid do Buttons. Cada Button tem uma cor de fundo e borda diferente. Eu quero então ter um UserControl chamado WidgetPanel que mantém um Grid do Widgets.

Para cada Widget definição dentro do WidgetPanel, Eu gostaria de poder definir o BorderBrush e Background de cada botão individual (nomeado button0, button1, button2 respectivamente) propriedade via XAML. Também gostaria de alterar programaticamente esses valores nos eventos a partir do código atrás em WidgetPanel.xaml.cs.

Aqui está o XAML e o código por trás de cada objeto:

XAML para 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>

Código atrasado para Widget

using System.Windows.Controls;

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

XAML para 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>

Código atrasado para WidgetPanel:

using System.Windows.Controls;

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

Respostas:

1 para resposta № 1

No Widget A classe define um conjunto de propriedades que afeta o estilo dos botões internos. Por exemplo, para 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
}

Em XAML de Widget:

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

Em XAML de WidgetPanel:

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

Claro, você pode definir essa propriedade a partir do code-behind de WidgetPanel:

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

0 para resposta № 2

Não tenho certeza se a ideia abaixo funciona. Experimente o código abaixo e ele pode resolver sua dúvida.

Ligue o evento Focused de Grid carregado dentro de um WidgetPanel UserControl em WidgetPanel.xaml.cs como abaixo,

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.
}

Acabo de compartilhar minha opinião. Eu não verifiquei o código acima. Que te ajude.