【发布时间】:2020-01-07 11:19:08
【问题描述】:
我正在寻找在转换器之前发生的事件。
我的意思是,我有一个自定义的UserControl,它包含一个ListView。它的ListViewItem 有一些元素,例如<TextBlock Foreground="{x:Bind IsPlaying, Converter={StaticResource RowColorConverter}, Mode=OneWay} />"。
这个控件被多次使用。我有一个名为 CurrentTheme 的静态 ElementTheme 变量,该变量在加载该控件时设置。
问题就在这里。 TextBlock 的Converter 使用那个CurrentTheme 来判断Foreground。然而,Loading 事件并不总是在Converter 之前触发,这意味着TextBlock 的Foreground 将根据旧的CurrentTheme 进行判断。正确地说,当Converter 首次加载时,加载事件发生在转换器之前。加载后,转换器几乎总是在加载之前被调用。
我应该如何解决这个问题?
---更新---
我使用的转换器:
class RowColorConverter : Windows.UI.Xaml.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value.Equals(true) ? Helper.GetHighlightBrush() :
PlaylistControl.CurrentTheme == ElementTheme.Dark ? Helper.WhiteSmokeBrush : Helper.BlackBrush;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return null;
}
}
我的使用代码:Interaction.Behaviors
<Interactivity:Interaction.Behaviors>
<Interactions:DataTriggerBehavior
Binding="{x:Bind IsPlaying}"
ComparisonCondition="Equal"
Value="True">
<Interactions:ChangePropertyAction
PropertyName="Foreground"
TargetObject="{Binding ElementName=ArtistTextBlock}"
Value="{StaticResource SystemColorHighlightColor}" />
</Interactions:DataTriggerBehavior>
<Interactions:DataTriggerBehavior
Binding="{x:Bind IsPlaying}"
ComparisonCondition="Equal"
Value="False">
<Interactions:DataTriggerBehavior
Binding="{Binding CurrentTheme}"
ComparisonCondition="Equal"
Value="Dark">
<Interactions:ChangePropertyAction
PropertyName="Foreground"
TargetObject="{Binding ElementName=ArtistTextBlock}"
Value="White" />
</Interactions:DataTriggerBehavior>
<Interactions:DataTriggerBehavior
Binding="{Binding CurrentTheme}"
ComparisonCondition="NotEqual"
Value="Dark">
<Interactions:ChangePropertyAction
PropertyName="Foreground"
TargetObject="{Binding ElementName=ArtistTextBlock}"
Value="Black" />
</Interactions:DataTriggerBehavior>
</Interactions:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
【问题讨论】:
标签: c# xaml uwp win-universal-app