【问题标题】:Color Binding Not Working for Data Templates in Win8 App颜色绑定不适用于 Win8 应用程序中的数据模板
【发布时间】:2013-03-19 09:35:39
【问题描述】:

我目前正在尝试将某种颜色主题功能添加到我正在使用的 Win8 应用程序中......我已经从 vm 进行绑定,并且一切都适用于静态 UI 元素。但是,我将一些注释(我的模型)添加到数据库中,它们也出现在屏幕上的 GridView 中。

但是在为 GridView ItemTemplate 声明的 DataTemplate 中,颜色绑定根本不起作用……

我的模板如下所示:

<Grid Grid.Row="3" HorizontalAlignment="Left" Width="200" Height="200">
    <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="60"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Border Grid.Row="0"  Background="Lavender"  Opacity="50"/>
    <ScrollViewer Grid.Row="0">
        <TextBlock Grid.Row="0" Text="{Binding Content}" Foreground="DodgerBlue" />
    </ScrollViewer>
    <Border Grid.Row="1"  Background="DodgerBlue" Opacity="70"/>
    <ScrollViewer Grid.Row="1">
        <TextBlock Grid.Row="1" Text="{Binding Subject}" Foreground="LightBlue" />
    </ScrollViewer>
    <Border Grid.Row="2" Background="DodgerBlue" Opacity="70"/>
    <TextBlock Grid.Row="2" Text="{Binding Importance}" Foreground="Black"  FontSize="{StaticResource ComboBoxArrowThemeFontSize}" />
</Grid>

我尝试的只是将Foreground="DodgerBlue" 改为Foreground="{Binding ColorTheme}",但没有效果,SolidColorBrush 不是从 vm 获取的......

有什么解决方法吗?

非常感谢。

【问题讨论】:

  • ColorTheme 属性的类型是什么?
  • 调试时输出窗口是否有任何错误?
  • ColorTheme 是 SolidColorBrush 类型。没有发现错误,只是绑定似乎不起作用,显示白色文本...

标签: xaml binding windows-8 colors windows-runtime


【解决方案1】:

颜色绑定(或更好的:画笔绑定)应该可以正常工作 - 在GridView.ItemTemplate 场景和其他地方。

您没有提供足够的信息来确定为什么它在您的情况下不起作用,但这是我刚刚尝试过的一个小示例:

GridView 放入你的页面:

<GridView Width="600" Height="200" ItemsSource="{Binding GridItems}"
          x:Name="GridViewName">
    <GridView.ItemTemplate>
        <DataTemplate>
            <TextBlock Width="50" Height="50" 
                       Foreground="{Binding Color}" Text="{Binding Text}" />
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

为您的页面查看绑定为DataContext 的模型类:

public class ViewModel
{
    public ViewModel()
    {
        GridItems = new List<GridItem>
            {
                new GridItem {Text = "First", Color = new SolidColorBrush(Colors.White)},
                new GridItem {Text = "Second", Color = new SolidColorBrush(Colors.Yellow)},
                new GridItem {Text = "Third", Color = new SolidColorBrush(Colors.Green)},
                new GridItem {Text = "Fourth", Color = new SolidColorBrush(Colors.Red)},
            };
    }
}

GridItem类:

public class GridItem
{
    public string Text { get; set; }
    public Brush Color { get; set; }
}

结果:

编辑:

我需要指出,内部数据模板DataContext 设置为绑定集合的当前项(在我的情况下为GridItems)而不是页面DataContext(在我的情况下为ViewModel)。

这意味着通过将{Binding Color} 设置为控件属性,您将绑定到GridItem.Color 而不是ViewModel.Color。要使后者工作,您首先需要使用 ElementName 语法访问父 DataContext,正如您在自己的答案中已经建议的那样:{Binding DataContext.ColorTheme, ElementName=GridViewName} - 这绑定到页面上的命名元素并允许您访问其属性, DataContext 在这种情况下是 ViewModel

【讨论】:

    【解决方案2】:

    虽然我并没有真正找到为什么以下工作的实际解释(我希望有更多经验的人,也许可以解释我),这个post,似乎对我有用。

    不太确定,但这是 DataContext 的问题,所以我没有尝试这样的绑定:Foreground={Binding ColorTheme},而是改为:Foreground={Binding DataContext.ColorTheme, ElementName=MyGridViewName}"

    【讨论】:

    • 我已根据您在此处提供的信息更新了我的答案。它对您不起作用,因为您的 ColorTheme 属性不像其他绑定属性(例如 ImportanceSubject)那样位于单个项目上,而是在集合级别。
    猜你喜欢
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 2019-11-05
    • 2011-12-20
    相关资源
    最近更新 更多