如何在DataTemplate中使用窗口级数据绑定样式

时间:2017-09-03 18:14:44

标签: c# wpf xaml

在WPF项目中,我的viewmodel有一些我绑定到样式的常规属性。然后我想在DataTemplate中使用该样式,我从viewmodel绑定一个集合。

数据绑定样式在DataTemplate外部按预期工作,但不适用于内部。在调试时我可以看到它正在查找集合对象中的常规属性,所以我的问题是,我如何在一个DataTemplate中获取viewmodel中的属性。我想我必须使用RelativeSource绑定,但我无法让它工作。

这个快速的应用程序应该显示我想要做的事情:

MainWindow.xaml

<Window x:Class="StyleTest.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:StyleTest"
        mc:Ignorable="d"
        Title="Test" 
        SizeToContent="WidthAndHeight">
    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="Header">
            <Setter Property="FontSize" Value="{Binding FontSize}" />
            <Setter Property="Foreground" Value="{Binding Foreground}" />
        </Style>
        <DataTemplate x:Key="UserTemplate">
            <StackPanel>
                <TextBlock Style="{StaticResource Header}" Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid Margin="20">
        <StackPanel>
            <ItemsControl Name="Itemscontrol" ItemsSource="{Binding Users}" ItemTemplate="{StaticResource UserTemplate}" />
            <TextBlock Style="{StaticResource Header}">Style this.</TextBlock>
        </StackPanel>
    </Grid>
</Window>

MainWindow.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;

namespace StyleTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Model m = new Model {
                FontSize = 28,
                Foreground = new SolidColorBrush(Colors.Orange),
                Users = new List<User>() };

            m.Users.Add(new User() { Name = "Mambo No. 1" });
            m.Users.Add(new User() { Name = "Right Hand Rob" });
            m.Users.Add(new User() { Name = "Perry Junior" });

            this.DataContext = m;
        }
    }

    public class Model
    {
        private int fontSize;
        public int FontSize { get => fontSize; set => fontSize = value; }

        private SolidColorBrush foreground;
        public SolidColorBrush Foreground { get => foreground; set => foreground = value; }

        private List<User> users;
        public List<User> Users { get => users; set => users = value; }
    }

    public class User
    {
        public string Name { get; set; }
    }
}

1 个答案:

答案 0 :(得分:1)

我想你想要这样的东西:

<Style TargetType="TextBlock" x:Key="Header">
    <Setter Property="FontSize" Value="{Binding  Path=DataContext.FontSize, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
    <Setter Property="Foreground" Value="{Binding Path=DataContext.Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</Style>