custom controles control c# wpf xaml user-controls

controles - c#wpf Style Control en UserControl



custom control wpf (1)

Tengo una ventana principal con un UserControl. Quiero cambiar el fondo de ListBox que está en UserControl. Pero el Estilo solo se aplica al UserControl y no al Control interno.

Más tarde quiero modificar los ListBoxItems de extern ..

Ventana principal

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="279.716" Width="279.784" DataContext="{Binding RelativeSource={RelativeSource Self}}"> <Window.Resources> </Window.Resources> <Grid> <Grid.Resources> <Style TargetType="{x:Type local:UserControl1}"> <Setter Property="Background" Value="Black"></Setter> <Style.Resources> <Style TargetType="{x:Type ListBox}"> <Setter Property="Background" Value="Red"></Setter> </Style> </Style.Resources> </Style> </Grid.Resources> <local:UserControl1 Margin="47,22,34,46"></local:UserControl1> </Grid>

XAML

<UserControl x:Class="WpfApplication1.UserControl1" 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:WpfApplication1" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <ListBox Background="Aqua" x:Name="listBox" HorizontalAlignment="Left" Height="192" Margin="54,44,0,0" VerticalAlignment="Top" Width="192"/> </Grid>


En realidad no necesitas un estilo para eso. Debe vincular la propiedad background del ListBox a la propiedad background del UserControl:

<UserControl x:Class="TestAppWPF.UserControl1" 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:TestAppWPF" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <ListBox Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=Background}" x:Name="listBox" HorizontalAlignment="Left" Height="192" Margin="54,44,0,0" VerticalAlignment="Top" Width="192"/> </Grid>

y la persona que llama debería verse así:

<Window x:Class="TestAppWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TestAppWPF" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <local:UserControl1 Margin="47,22,34,46" Background="Brown"></local:UserControl1> </Grid> </Window>

Y el resultado es:

Si desea utilizar un estilo para todos sus controles personalizados para fondo, debe usar esta sección de código (después de aplicar el enfoque sugerido):

<Style TargetType="{x:Type local:UserControl1}"> <Setter Property="Background" Value="Red"></Setter> </Style>

Ahora, si desea cambiar el fondo de los elementos, es un poco más complicado. Tienes que crear un estilo para el elemento ListBoxItem que debería verse a continuación:

<Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Background" Value="Yellow" /> </Style>

Pero es probable que desee controlar el color desde fuera del control, por lo que necesita una propiedad de dependencia.

En UserControl1.xaml.cs debe definir:

public static readonly DependencyProperty ItemsBackgroundProperty = DependencyProperty.Register("ItemsBackground", typeof(string), typeof(UserControl1), new FrameworkPropertyMetadata()); public string ItemsBackground { get { return (string)GetValue(ItemsBackgroundProperty); } set { SetValue(ItemsBackgroundProperty, value); } }

Y el estilo se modificará para usar esta propiedad:

<UserControl.Resources> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=ItemsBackground}" /> </Style> </UserControl.Resources>

Ahora, lo único que tiene que establecer es esta propiedad cuando usa su control:

<local:UserControl1 Margin="47,22,34,46" ItemsBackground="Yellow" ></local:UserControl1>

El resultado :