How to change value of a defined resource in xaml?

冷暖自知 提交于 2019-12-11 01:26:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!