【发布时间】:2019-02-23 05:18:24
【问题描述】:
我正在从代码隐藏更新 App ThemeResource。它正在更改应用程序主题,但 TextBox BorderBrush 属性未更新。
我有一个自定义资源 MyBorderBrush 用于 Dark 和 Light 我在 App.xaml 中定义的主题。
Xaml:
<StackPanel>
<TextBox PlaceholderText="My PlaceholderText" Height="100" Width="500" HorizontalAlignment="Center" Style="{StaticResource NoHighlightTextBoxStyle}" BorderBrush="{Binding IsError, Converter={ThemeResource BorderBrushColorConverter}}" VerticalAlignment="Center" ></TextBox>
<Button Content="Change Theme" Click="Button_Click"></Button>
</StackPanel>
代码背后:
private void Button_Click(object sender, RoutedEventArgs e)
{
this.RequestedTheme = this.RequestedTheme == ElementTheme.Light ? ElementTheme.Dark : ElementTheme.Light;
}
编辑
我认为问题可能出在背后的代码或定义资源上,因此我只共享了最少的代码以重现该问题。但正如@Ashiq 所指出的,问题出在TextBox 上。实际上,问题是我将BorderBrush 属性绑定到转换器以获得正确的值,但是在更改主题时边框颜色没有改变。
转换器:
public class BorderBrushColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var isError = value as bool? ?? false;
return isError
? Application.Current.Resources["MyBorderBrushMandatory"] as SolidColorBrush
: Application.Current.Resources["MyBorderBrush"] as SolidColorBrush;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
App.xaml
<ResourceDictionary x:Key="Light" >
<Color x:Key="MyBorder">#6b6b6b</Color>
<SolidColorBrush x:Key="MyBorderBrush" Color="{ThemeResource MyBorder}" />
<Color x:Key="MyBorderMandatory">#ff0000</Color>
<SolidColorBrush x:Key="MyBorderBrushMandatory" Color="{ThemeResource MyBorderMandatory}" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark" >
<Color x:Key="MyBorder">#c85332</Color>
<SolidColorBrush x:Key="MyBorderBrush" Color="{ThemeResource MyBorder}" />
<Color x:Key="MyBorderMandatory">#FFD700</Color>
<SolidColorBrush x:Key="MyBorderBrushMandatory" Color="{ThemeResource MyBorderMandatory}" />
</ResourceDictionary>
【问题讨论】:
-
您是否使用 INotifyPropertyChanged 事件到 IsError 属性?
-
是的,我使用
INotifyPropertyChanged表示 IsError。