/ / Jak ustawić właściwości komponentu UserControls w WPF XAML - C #, wpf, xaml, user-controls

Jak ustawić właściwości składnika UserControls w WPF XAML - c #, wpf, xaml, user-controls

Szukam zwięzłego fragmentu przykładowego kodu, który wykonuje następujące czynności:

Z wyższego poziomu UserControl, Chciałbym zmienić właściwość obiektu (np Button) w ramach sub UserControl przez XAML.

Na przyk UserControl nazywa Widget który zawiera Grid z Buttons. Każdy Button ma inny kolor tła i obramowania. Następnie chcę mieć UserControl nazywa WidgetPanel który utrzymuje Grid z Widgets.

Dla każdego Widget definicja w ramach WidgetPanel, Chciałbym móc ustawić BorderBrush i Background każdego przycisku (o nazwie button0, button1, button2 odpowiednio) za pośrednictwem XAML. Chciałbym również programowo zmienić te wartości w zdarzeniach z kodu znajdującego się za WidgetPanel.xaml.cs.

Oto kod XAML i kod dla każdego obiektu:

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

Kod za Widget

using System.Windows.Controls;

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

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

Kod za WidgetPanel:

using System.Windows.Controls;

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

Odpowiedzi:

1 dla odpowiedzi № 1

w Widget class definiuje zestaw właściwości wpływających na styl przycisków wewnętrznych. Na przykład dla 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
}

W XAML z Widget:

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

W XAML z WidgetPanel:

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

Oczywiście możesz ustawić tę właściwość z poziomu kodu WidgetPanel:

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

0 dla odpowiedzi nr 2

Nie jestem pewien, czy poniższy pomysł zadziała. Wypróbuj poniższy kod, a może on rozwiązać Twoje zapytanie.

Połącz zdarzenie Focused Grid załadowane wewnątrz WidgetPanel UserControl w WidgetPanel.xaml.cs, jak poniżej,

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

Właśnie podzieliłem się swoją opinią. Nie sprawdziłem powyższego kodu. Niech ci to pomoże.