[英]Dynamically change a static resource
在我的App.xaml中,我有兩個資源
<x:Double x:Key="MasterGridSize">150</x:Double>
<DataTemplate x:Key="MasterGridItemTemplate">
<Grid Width="{StaticResource MasterGridSize}" Height="{StaticResource MasterGridSize}">
</Grid>
</DataTemplate>
當我更改MasterGridSize
的值時,它不會更改網格大小-符合預期。 我該如何實現?
我嘗試過ThemeResource
,但是當主題更改時,它至少在更改。
DynamicResource
不可用。
我不想將MasterGridSize添加到ViewModel中,因為這會導致ViewModels與某些更新代碼之間存在某些依賴關系。
還有其他想法嗎?
編輯:直接編輯DataTemplate也可能很合理-我將如何實現?
您需要使用值持有者,例如UWP Community Toolkit中的BindableValueHolder 。
<helpers:BindableValueHolder x:Key="MasterGridSize">
<helpers:BindableValueHolder.Value>
<x:Double>150</x:Double>
</helpers:BindableValueHolder.Value>
</helpers:BindableValueHolder>
<DataTemplate x:Key="MasterGridItemTemplate">
<Grid Width="{Binding Value, Source={StaticResource MasterGridSize}}" Height="{Binding Value, Source={StaticResource MasterGridSize}}" />
</DataTemplate>
源代碼在這里 !
您的資源聲明得很好嗎? 如果是,請嘗試按以下方式嘗試GridLength:
<Page.Resources>
<GridLength x:Key="FirstSize">5*</GridLength>
<GridLength x:Key="SecondSize">4*</GridLength>
</Page.Resources>
<DataTemplate x:Key="MasterGridItemTemplate">
<Grid Width="{StaticResource FirstSize}" Height="{StaticResource SecondSize}">
</Grid>
</DataTemplate>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.