首页 文章

如何将两个不同的属性绑定到文本块文本和前景?

提问于
浏览
0

我已经为多个listboxitems和一些文本块创建了一个模板 . 在设置中,用户可以将应用程序的背景更改为黑色或白色(然后文本块前景色应相应地更改相反) . 我如何 bind 文本块 text to one property (项目列表(observablecollection))和 foreground to another property (带颜色的转换器)不在同一个datacontext中(但在settings-datacontext中)?

我想做什么:

<DataTemplate x:Key="ArticleItemTemplateClassic">
        <Grid>
            <!-- ... --->
             <TextBlock Text="{Binding Description}"
                        Foreground="{Binding SettingsFile.BlackBackgroundEnabled,
                        Converter={StaticResource InverseBackgroundColorConverter}}"/>
            <!-- The Context of the Foreground (SettingsFile.BlackBackgroundEnabled) -->
            <!-- should be not the same as where I bind Description -->
            </StackPanel>
            <!-- ... --->
        </Grid>
    </DataTemplate>

谢谢!

2 回答

  • 0

    为此,您需要指定Foreground属性的绑定源 . 这可以通过多种方式完成,但一个示例是将Settings类公开为资源 .

    例如:

    <Grid x:Name="LayoutRoot">
        <Grid.Resources>
            <!-- If you want to use SettingsFile as a static, you might want to expose an accessor/wrapper class for it here instead. -->
            <settings:SettingsFile x:Name="SettingsFileResource" />
        </Grid.Resources>
        <ListBox ItemsSource="{Binding MyItems}">
            <ListBox.ItemTemplate>
                <DataTemplate x:Key="ArticleItemTemplateClassic">
                    <Grid>
                        <!-- ... -->
                        <TextBlock Text="{Binding Description}"
                                   <!-- Now change your Binding Path to the target property, and set the source to the resource defined above. -->
                        Foreground="{Binding BlackBackgroundEnabled, Source={StaticResource SettingsFileResource}, Converter={StaticResource InverseBackgroundColorConverter}}"/>
    
                        <StackPanel />
                        <!-- ... -->
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    

    替代方案,使用AttachedProperty可能更清洁 . 例如:

    public static bool GetBlackBackgroundEnabled(DependencyObject obj)
    {
        return (bool)obj.GetValue(BlackBackgroundEnabledProperty);
    }
    
    public static void SetBlackBackgroundEnabled(DependencyObject obj, bool value)
    {
        obj.SetValue(BlackBackgroundEnabledProperty, value);
    }
    
    // Using a DependencyProperty as the backing store for BlackBackgroundEnabled.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BlackBackgroundEnabledProperty =
        DependencyProperty.RegisterAttached("BlackBackgroundEnabled", typeof(bool), typeof(Control), new PropertyMetadata(false, (s, e) =>
            {
                Control target = s as Control;
                SolidColorBrush brush = new SolidColorBrush();
    
                // Logic to determine the color goes here
                if (GetBlackBackgroundEnabled(target))
                {
                    brush.Color = something;
                }
                else
                {
                    brush.Color = somethingElse;
                }
    
                target.Foreground = brush;
            }));
    

    然后你会像这样使用它:

    <TextBlock settings:SettingsFile.BlackBackgroundEnabled="True" />
    
  • 0

    如果您被迫这样做,您可以为每个项目明确指定不同的 DataContext . 虽然我不确定为什么你有两个属性与同一个 DataTemplate 的外观一致,位于不同的容器中 .

相关问题