我对Xaml相当陌生。
我正在学习UWP(通用Windows平台),并且我有更多的按钮,我想将它们的Background属性绑定到ViewModel的某个属性,该属性在某些事件中将会更改。如何在样式中设置绑定?
我实现了INotifyPropertyChanged的,一切都会OK(按钮的颜色变化),当我绑定在按键声明Background属性在XAML:
<Button Content="0" Grid.Column="0" Grid.Row="5"
Background="{Binding ButtonColor, Source={StaticResource AppViewModel}}" Style="{StaticResource BasicButton}"/>
静态资源AppViewModel在App.xaml中的资源:
<Application.Resources>
<viewModel:AppViewModel x:Key="AppViewModel" />
</Application.Resources>
我不知道是怎么确定申报视图模型为App.xaml中,但它是一个解决方案,我发现有全局变量(该变量保持视图模型内)。
现在回到我的问题:颜色变量时
<Style x:Key="BasicButton" TargetType="Button">
<Setter Property="Background" Value="{Binding ButtonColor, Source={StaticResource AppViewModel}}" />
</Style>
但现在: 因为我不想在每一个按钮的背景绑定,我试图将其添加在这样的风格在运行应用程序期间正在改变,UI不再更新。 样式中的绑定属性似乎不响应变量的变化。
我在做什么错? 谢谢你的回答。
你是否尝试使用viewmodel作为DynamicResource? –
DynamicResource不存在于uwp/windows store应用程序api –
它不起作用的原因是因为样式被添加为staticResource,因此即使它在封面下发生更改,它也会通知链接的按钮。 –