c#wpf UserControl中的样式控件

时间:2016-10-13 08:31:37

标签: c# wpf xaml user-controls

我有一个带有UserControl的MainWindow。我想更改UserControl中ListBox的背景。但Style只适用于UserControl,而不适用于内部Control。

后来我想从extern修改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>

Preview

1 个答案:

答案 0 :(得分:0)

你实际上并不需要一种风格。您必须将ListBox的background属性绑定到UserControl的background属性:

$('#ketenagalistrikan-pembenahan-layanan-chart').highcharts({

chart: {
    type: 'scatter',
    zoomType: 'xy'
},
title: {
    text: 'Pembenahan Dimensi Layanan'
},
subtitle: {
    text: 'Source: rad-research.com'
},
labels: {
    items : [
    {
        html : 'Prioritas 4',
        style : {
            left : '380%',
            top : '60px',
            fontSize : '10px'
        }
    },
    {
        html : 'Prioritas 3',
        style : {
            left : '660%',
            top : '60px',
            fontSize : '10px'
        }
    }
    ]
},
xAxis: {
    title: {
        enabled: true,
        text: 'Kepentingan'
    },
    min: -0.5,
    max: 39.3,
    startOnTick: true,
    endOnTick: true,
    showLastLabel: true,
    plotLines: [{
        color: 'black',
        dashStyle: 'dot',
        width: 2,
        value: 12.7,
        zIndex: 3

    },{
        color: 'black',
        dashStyle: 'dot',
        width: 2,
        value: 26,
        zIndex: 3
    },{
        color: 'black',
        dashStyle: 'dot',
        width: 2,
        value: 39.3,
        zIndex: 3
    }]
},
yAxis: {
    title: {
        text: 'Kepuasan'
    },
    tickInterval: 8.9,
    startOnTick: true,
    endOnTick: true,
    showLastLabel: true,
    plotLines: [{
        color: 'black',
        dashStyle: 'dot',
        width: 2,
        value: 46.2,
        zIndex: 3
    },{
        color: 'black',
        dashStyle: 'dot',
        width: 2,
        value: 55.2,
        zIndex: 3
    }]
},
plotOptions: {
    scatter: {
        marker: {
            radius: 5,
            states: {
                hover: {
                    enabled: true,
                    lineColor: 'rgb(100,100,100)'
                }
            }
        },
        states: {
            hover: {
                marker: {
                    enabled: false
                }
            }
        },
        tooltip: {
            headerFormat: '<b>{series.name}</b><br>',
            pointFormat: '{point.x} cm, {point.y} kg'
        }
    }
},
series: [{
    name: 'Marketing',
    data: [[8.75, 45.8]]
},{
    name: 'Sales',
    data: [[15.0, 43.6]]
},{
    name: 'Aktivasi/Instalasi',
    data: [[-0.3, 53.2]]
},{
    name: 'Contact Center',
    data: [[1, 52.2]]
},{
    name: 'Customer Account',
    data: [[-0.3, 60.1]]
},{
    name: 'Field Support',
    data: [[30.5, 40.3]]
},{
    name: 'Billing',
    data: [[37.5, 59.2]]
}]

并且调用者应该看起来像:

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

结果是:

enter image description here

如果要为所有自定义控件使用样式作为背景,则应使用此部分代码(在应用建议的方法后):

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

现在,如果你想改变项目的背景,那就有点复杂了。您必须为 ListBoxItem 项创建一个样式,该样式应如下所示:

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

但是你可能想控制控件外部的颜色,所以你需要一个依赖属性。

在UserControl1.xaml.cs中,您必须定义:

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

该样式将被修改为使用此属性:

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); }
        }

现在,您使用控件时唯一需要设置的是此属性:

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

结果:

enter image description here