UWP : How to use x:bind in ResourceDictionary?

标签 uwp resourcedictionary xbind

我想在 UWP 中使用 ResourceDictionary 就像我在 WPF 中使用的那样 在 WPF 中,我在 ResourceDictionary 文件(*Style.xaml)中执行此操作

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:models="using:NWP.Models">

<Style x:key="MenuContent" TargetType="ContentControl">
    <Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="ContentControl">
            <controls:DockPanel>
                <ItemsControl ItemsSource="{x:Bind How-Can-I-Bind-Collection-Here?}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate x:DataType="models:MenuItemModel">
                            <RadioButton GroupName="MenuItems" IsChecked="{x:Binding IsChecked, Mode=TwoWay}" MinHeight="0" MinWidth="0" Command="{Binding Command}" CommandParameter="{Binding}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>

                <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </controls:DockPanel>
        </ControlTemplate>
    </Setter.Value>
</Style>

</ResourceDictionary>

然后我可以在我的页面中使用这种样式:

<ContentControl Style="{StaticResource MenuContent}">
    <StackPanel Orientation="Vertical">
        <TextBox/>
        <PasswordBox/>
        <Button Content="Login"/>
    </StackPanel>
</ContentControl>

但是现在,我在努力解决如何使用 x:bind 为 ResourceDictionary 中的 ItemsControl 中的 ItemsSource 提供源:

<ItemsControl ItemsSource="{x:Bind How-Can-I-Bind-Collection-Here?}">

我的问题是,如何解决这个问题?

最佳答案

添加@Bite的建议,我需要为您解释更多信息。在那份文件中,它说:

if you use {x:Bind} in a resource dictionary then the resource dictionary needs to have a code-behind class.

  1. 从文档页面来看,它听起来好像可以工作,像任何其他 x:Bind 一样使用支持类作为绑定(bind)源,但编译器明确拒绝样式中的任何 x:Bind。它根本不受支持。因此,您不能在样式中使用 x:Bind,即使对于样式中定义的 DataTemplate 也是如此。
  2. 为了使用 x:Bind 中定义的 DataTemplate ResourceDictionary,您需要为其创建一个代码隐藏类。 由于第 1 点,此模板仍必须位于任何 Style 之外,但您需要 x:Bind 的支持类才能从 资源字典。
  3. 要使用基于 ResourceDictionary 的方法使项目源绑定(bind)正常工作,您需要使用旧版 {Binding} 而不是 {x:Bind}。
  4. 由于我们必须使用旧版 {Binding} 才能使其正常工作,因此必须通过设置和/或调整 ContentControl 的 DataContext 来自定义项目源。

然后,我制作了一个简单的代码示例供您引用:

<ResourceDictionary
x:Class="AppStyle.MenuDictionary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppStyle"
xmlns:System="using:System">

<Style x:Key="MenuContent" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <ItemsControl ItemsSource="{Binding MenuItems}" ItemTemplate="{StaticResource MenuItemDataTemplate}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                    <ContentPresenter Grid.Row="1" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<DataTemplate x:Key="MenuItemDataTemplate" x:DataType="System:String">
    <RadioButton GroupName="MenuItems" MinHeight="0" MinWidth="0" Content="{x:Bind}" />
</DataTemplate>

public sealed partial class MenuDictionary : ResourceDictionary
{
    public MenuDictionary()
    {
        this.InitializeComponent();
    }
}
<Page
x:Class="AppStyle.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppStyle"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <local:MenuDictionary/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ContentControl Style="{StaticResource MenuContent}">
        <StackPanel>
            <TextBlock>Test</TextBlock>
            <TextBox />
        </StackPanel>
    </ContentControl>
</Grid>

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }
    public IEnumerable<string> MenuItems => new string[] { "Page Item 1", "Page Item 2", };
}

关于UWP : How to use x:bind in ResourceDictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48030101/

相关文章:

azure - azure 应用服务身份验证模块是否支持 azure ad b2c?

wpf - 如何通过 generic.xaml 中定义的键访问资源

wpf - 复合WPF(Prism)模块资源数据模板

mvvm - TextBox Keydown方法被调用两次

c# - 如何将 c# uwp 应用程序设置为默认应用程序以打开某些文件?

c# - UWP StorageLibraryContentChanged删除多个文件时触发

c# - 在 ScrollViewer (UWP) 中移动图像

wpf - 将标记扩展编写为嵌套元素

c# - 使用 x :Bind 通过 Converter 绑定(bind)到当前 DataContext

c# - 通过x :bind in UWP绑定(bind)数据到ListView