我正在从代码隐藏更新应用程序ThemeResource
。它正在更改应用程序主题,但未更新TextBox BorderBrush
属性。
我有一个Dark
和Light
主题的自定义资源MyBorderBrush
,这是我在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>
发布于 2018-09-19 14:28:20
您正在运行时进行动态主题更改。因此,将StaticResource更改为ThemeResource。就这样。
从…
BorderBrush="{StaticResource MyBorderBrush}"
至
BorderBrush="{ThemeResource MyBorderBrush}"
谢谢你。
https://stackoverflow.com/questions/52398960
复制相似问题