c# - 如何更改 xaml 中定义的资源的值?

标签 c# xaml windows-phone-8 windows-runtime windows-store-apps

我在 xaml 中有一个网格,它为其附加的弹出控件使用资源:

<Grid >
    <FlyoutBase.AttachedFlyout>
        <StaticResource ResourceKey="GridFlyout"/>
    </FlyoutBase.AttachedFlyout>

    .. other stuffs

</Grid>

我在页面中有一个定义的资源:

<Page.Resources>
    <MenuFlyout x:Key="GridFlyout">
        <MenuFlyoutItem Text="delete"/>
        <MenuFlyoutItem Text="like"/>
        <MenuFlyoutItem Text="edit"/>
    </MenuFlyout>

但在某些情况下我想为上面的网格设置以下资源:

<Page.Resources>
    <MenuFlyout x:Key="SecondaryGridFlyout">
        <MenuFlyoutItem Text="like"/>
    </MenuFlyout>

我怎样才能做到这一点?谢谢

最佳答案

如果您只是在代码中执行此操作,这是最简单的(并且完全受支持)。使用附加属性AttachedFlyout:

FlyoutBase.SetAttachedFlyout(theGrid, 
       (MenuFlyout) App.Current.Resources["SecondaryGridFlyout"]);
上例中的

theGrid 代表您要更改的Grid

<Grid x:Name="theGrid">
    <FlyoutBase.AttachedFlyout>
        <StaticResource ResourceKey="GridFlyout"/>
    </FlyoutBase.AttachedFlyout>
    <!-- ... other stuff -->
</Grid>

关于c# - 如何更改 xaml 中定义的资源的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23564189/

相关文章:

c# - Zxing 的 System.Drawing.Bitmap 中可能存在内存泄漏

silverlight - 更改 TargetNullValue 的 TextBlock 样式

c# - 以编程方式更改代码文件

c# - itextsharp 和图像大小

c# - 保持前端 Angular 5 和后端 Web API 代码分离的简单方法?

c# - 单击文本 block 时隐藏同级列表框

c# - XAML - C# - MenuItem 中的图片未显示

windows-phone-7 - 有关 Windows Phone 的信息(型号)

xaml - 如何允许用户在 Windows Phone 8 上移动\调整图片上的图像控件的大小

下次启动 Windows Phone 8 应用程序时不会保留 html5 本地存储