1

I'm currently trying to add some sort of a Color Theme feature to a Win8 App I'm working at... I've though of making a binding from a vm, and everything works fine for static UI elements. But, I'm adding some notes (my model) into a DB, and they also appear on the screen into a GridView.

But in the declared DataTemplate for the GridView ItemTemplate, the Color Binding will not work at all...

My template looks like this :

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

What I tried was simply instead of Foreground="DodgerBlue" to Foreground="{Binding ColorTheme}" but it had no effect, the SolidColorBrush was not acquired from vm....

Is there any workaround for this?

Many thanks in advance.

4

2 回答 2

0

颜色绑定(或更好:画笔绑定)应该可以正常工作 - 在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在我的情况下)而不是页面DataContextViewModel在我的情况下)。

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

于 2013-03-29T05:55:25.180 回答
0

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

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

于 2013-03-29T10:06:01.163 回答