问题描述
我有一个带有 UserControl 的 MainWindow.我想更改 UserControl 中 ListBox 的背景.但是 Style 只适用于 UserControl,而不适用于内部 Control.
I have a MainWindow with an UserControl. I want to change the background of the ListBox which is in the UserControl. But the Style is only applied to the UserControl and not on the inner Control.
稍后我想从外部修改 ListBoxItems..
<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>
推荐答案
实际上您不需要样式.您必须将 ListBox 的背景属性绑定到 UserControl 的背景属性:
You actually don't need a style for that. You have to bind the background property of the ListBox to the background property of the UserControl :
<UserControl x:Class="TestAppWPFStackOverFlow.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:TestAppWPFStackOverFlow"
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>
调用者应该看起来像:
<Window x:Class="TestAppWPFStackOverFlow.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:TestAppWPFStackOverFlow"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:UserControl1 Margin="47,22,34,46" Background="Brown"></local:UserControl1>
</Grid>
</Window>
结果是:
如果您想为所有自定义背景控件使用一种样式,则应使用此部分代码(在应用建议的方法之后):
If you want to use a style for all your custom controls for background, you should use this section of code (after you apply the suggested approach):
<Style TargetType="{x:Type local:UserControl1}">
<Setter Property="Background" Value="Red"></Setter>
</Style>
现在,如果您想更改项目的背景,那就有点复杂了.您必须为 ListBoxItem 项目创建一个样式,如下所示:
Now, if you want to change the background of the items it is a little bit more complicated. You have to create a style for the ListBoxItem item which should look like below :
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Yellow" />
</Style>
但是你可能想从控件外部控制颜色,所以你需要一个依赖属性.
But you probably want to control the color from outside the control, so you need a dependency property.
在 UserControl1.xaml.cs 中,您必须定义:
In UserControl1.xaml.cs you have to define :
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); }
}
并且样式将被修改以使用此属性:
And the style will be modified to use this property :
<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>
现在,当您使用控件时,您唯一需要设置的就是这个属性:
Now, the only thing that you have to set is this property when you use your control :
<local:UserControl1 Margin="47,22,34,46" ItemsBackground="Yellow" ></local:UserControl1>
结果:
这篇关于UserControl中的c#wpf样式控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!