gpt4 book ai didi

c# - UWP 应用程序中的绑定(bind)和转换器未设置背景颜色

转载 作者:行者123 更新时间:2023-11-30 21:37:05 25 4
gpt4 key购买 nike

我尝试使用绑定(bind)到绑定(bind)项 (TimeEntry) 的属性来设置 UWP 应用中列表项的背景颜色(或出于测试目的设置 TextBlock 的文本前景色)。

这是绑定(bind)到 TimeEntries 集合的 ListView 的 Xaml(倒数第二行的相关 TextBlock):

...
<Page.Resources>
<local:TimeEntryTypeColorConverter x:Key="TimeEntryTypeColorConverter" />
</Page.Resources>
...
<StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1">
<TextBlock Text="Xy:" />
<ListView>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
....
</Grid.ColumnDefinitions>
<TextBlock Text="Test" Grid.Row="0" Grid.Column="0" Foreground="{Binding Type, Converter={StaticResource TimeEntryTypeColorConverter}}" />
...

TimeEntry 类有一个“TimeEntryType”枚举和一个“Type”属性:

public enum TimeEntryType
{
Unknown,
Standard,
Break
}

public TimeEntryType Type
{
get
{
if (_isBreak)
{
return TimeEntryType.Break;
}
return TimeEntryType.Standard;
}
}

这就是这个属性/枚举的转换器的样子:

public class TimeEntryTypeColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return null;
var timeEntryType = (TimeEntry.TimeEntryType)value;
if (timeEntryType == TimeEntry.TimeEntryType.Break)
return Colors.LightGray;

return Colors.Transparent;
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}

我对 ListView 项集合的 TimeEntry 对象有其他有效的绑定(bind)。而且这种绑定(bind)似乎也有效,因为调试器向我显示正在使用转换器,并且它还会转换为“LightGray”,例如“Break”。但是UI没有变化,其他绑定(bind)直接更新,所以绑定(bind)正常。

我不明白为什么 UI 没有更新为“LightGray”,尽管转换器似乎被正确使用并将此值作为前景色或背景色返回。

最佳答案

作为documentation显示; Foreground 需要一个 Brush 而不是 Color

您可以通过以下方式解决您的问题:

var color = // Select your color here //
var brush = new SolidColorBrush(color);

从本质上讲,颜色是不言自明的,但画笔是一种用于绘画的实际“ Material ”。

关于c# - UWP 应用程序中的绑定(bind)和转换器未设置背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47609757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com