问题
I have a grid in the xaml which uses a resource for its attached flyout:
<Grid >
<FlyoutBase.AttachedFlyout>
<StaticResource ResourceKey="GridFlyout"/>
</FlyoutBase.AttachedFlyout>
.. other stuffs
</Grid>
and I have a defined resource in the page:
<Page.Resources>
<MenuFlyout x:Key="GridFlyout">
<MenuFlyoutItem Text="delete"/>
<MenuFlyoutItem Text="like"/>
<MenuFlyoutItem Text="edit"/>
</MenuFlyout>
But in some conditions I want to set the following resource for the above grid:
<Page.Resources>
<MenuFlyout x:Key="SecondaryGridFlyout">
<MenuFlyoutItem Text="like"/>
</MenuFlyout>
How can I do that? thanks
回答1:
It's easiest (and fully supported) if you just do this in code. Using the attached property AttachedFlyout
:
FlyoutBase.SetAttachedFlyout(theGrid,
(MenuFlyout) App.Current.Resources["SecondaryGridFlyout"]);
theGrid
in the example above represents the Grid
you want to change.
<Grid x:Name="theGrid">
<FlyoutBase.AttachedFlyout>
<StaticResource ResourceKey="GridFlyout"/>
</FlyoutBase.AttachedFlyout>
<!-- ... other stuff -->
</Grid>
来源:https://stackoverflow.com/questions/23564189/how-to-change-value-of-a-defined-resource-in-xaml