我想在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?}">
我的问题是,如何解决这个问题?
答案 0 :(得分:3)
加入@Bite的建议,我需要为您解释更多信息。在该文件中,它说:
如果在资源字典中使用{x:Bind},则资源字典需要具有代码隐藏类。
然后,我做了一个简单的代码示例供您参考:
<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", };
}