绑定值到模板属性

时间:2018-01-26 16:14:53

标签: c# wpf

我在将值绑定到属性方面存在严重问题。

这是我的源代码。

public partial class MainPage : Page,Autodesk.Revit.UI.IDockablePaneProvider
{
    ....
    public System.Windows.Media.Brush BCol { get; set; }
    .....
}

<ListBox Style = "{DynamicResource ListBoxStyle}">
     ...
</ListBox>

<Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
   <Setter Property="Template">
     <Setter.Value>
         <ControlTemplate TargetType="{x:Type ListBox}">
              <Grid Background="{Binding  BCol}">
                   ...
              </Grid>
         </ControlTemplate>
     </Setter.Value>
   </Setter>
</Style>

我想将BCol绑定到网格背景但它不起作用。 我该怎么办?

2 个答案:

答案 0 :(得分:1)

根据BCol的设置位置,您可能需要实现INotifyPropertyChanged接口。您还需要确保绑定到分配给DataContext的视图模型。

您还可以创建DependencyProperty。我在下面发布了两个解决方案。 DependencyProperty可能是您想要的解决方案。

INotifyPropertyChanged解决方案:

XAML

<Page x:Class="WpfTest.MainPage"
      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:WpfTest"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="MainPage">

    <Grid>
        <Grid.Resources>
            <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBox}">
                            <Grid Background="{Binding  BCol}">
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>
        <ListBox Style = "{DynamicResource ListBoxStyle}">
        </ListBox>
    </Grid>
</Page>

代码背后:

using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfTest
{
    /// <summary>
    /// Interaction logic for MainPage.xaml
    /// </summary>
    public partial class MainPage : Page, INotifyPropertyChanged
    {
        private Brush bCol;

        public System.Windows.Media.Brush BCol
        {
            get { return bCol; }
            set
            {
                bCol = value; 
                RaisePropertyChanged("BCol");
            }
        }

        public MainPage()
        {
            InitializeComponent();
            DataContext = this; //This is a hack, you should really create a separate view model
            BCol=new SolidColorBrush(Colors.Blue);

        }

        public event PropertyChangedEventHandler PropertyChanged= delegate { };

        void RaisePropertyChanged(string name)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(name));
        }

    }
}

DependencyProperty解决方案

XAML

<Page x:Class="WpfTest.MainPage"
      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:WpfTest"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="MainPage">
    <Grid>
        <Grid.Resources>
            <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBox}">
                            <Grid Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                AncestorType={x:Type local:MainPage}}, Path=BCol}">
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>
        <ListBox Style = "{DynamicResource ListBoxStyle}">
        </ListBox>
    </Grid>
</Page>

背后的代码

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfTest
{
    /// <summary>
    /// Interaction logic for MainPage.xaml
    /// </summary>
    public partial class MainPage : Page
    {
        private Brush bCol;

        public static DependencyProperty BColProperty =DependencyProperty.Register("BCol", typeof(Brush),typeof(MainPage));
    public Brush BCol
    {
        get { return (Brush)this.GetValue(BColProperty); }
        set { this.SetValue(BColProperty, value); }
    }

    public MainPage()
        {
            InitializeComponent();
            BCol=new SolidColorBrush(Colors.Blue);

        }
    }
}

希望这有帮助。

答案 1 :(得分:0)

您需要使用所有xmlns的

将其添加到xaml的顶部
DataContext="{Binding RelativeSource={RelativeSource Self}}"