- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 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.
然后,我制作了一个简单的代码示例供您引用:
<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/
我已经在我的共享项目中毫无问题地创建了一个 ResourceDictionary。但是,我的某些样式会非常特别* Windows Phone 8.1 而不会在 Windows 8.1 中使用。由于 W
我有一些这样的代码 _images = new ResourceDictionary { Source = new Uri(@"pack://a
我的 App.xaml 中有以下代码集:
是否可以将一个资源字典添加到另一个资源字典中? 最佳答案 在 Dictionary2.xaml 中定义 MergedDictionaries(紧跟在打开的 ResourceDictionary 标记之
我有两个程序集,每个程序集都提供一组通用的样式和资源,我想将它们包含在我的应用程序中。我在 App.xaml 中使用合并字典为了加载它们,在运行时它们很好。不幸的是,这些资源不会在设计时加载,用有关无
我有资源字典文件(MenuTemplate.xaml、ButtonTemplate.xaml 等),我想在多个单独的应用程序中使用它们。我可以将它们添加到应用程序的程序集中,但如果我将这些资源编译在一
在我的WPF应用程序内部,我包括另一个项目的ResourceDictionary。
ONE TWO THREE 现在想使用 C# 在 WPF 中通过 code-behind 动态创建与上面相同的资源 ResourceDictionary。是否可以这样创建?
我有一个类库项目,它对其他几个项目是通用的。它的程序集名称是“MyCompany”,它的默认命名空间也是“”。 然后在这些其他项目之一(命名空间“MyCompany.Something”)中,我引用了
我需要动态更改 App.xaml 文件中的 ResourceDictionary。我尝试了以下代码: ResourceDictionary newRes = new ResourceDictionar
请提前见谅。第一个问题。 我正在开发一个 WPF 项目,在该项目中我在应用程序级别定义了一个简单的资源字典。 对 ResourceDictionary 的引用工作正常;我
我有以下 ResourceDictionary(缩写): 当我访问字典的 Keys 属性时,键的顺序如下: dokumentRibbonTabaustauschRibbon
我有一个名为 MyButton.xaml 的 ResourceDictionary,其中定义了 x:Type ButtonBase 的样式。 我知道如何使用此 ResourceDictionary 以
在下面的触发器中,只有前景 setter 在工作。我不明白为什么。 感谢您的帮助。 最佳答案 这不起作用的原因是因为默认的 Button 模板使用 Bu
我有这个应用程序结构: AppName Resources Languages en.xaml it.xaml 我正在尝试根据我
假设我的 Application.xaml 中有一些 ResourceDictionary 定义如下:
我在文件系统的某处有一个程序集,例如“C:\temp\test.dll”。在该程序集中有一个 ResourceDictionary,例如“abc.xaml”。 我怎样才能得到 ResourceDict
我的项目中有一个文件夹,Templates,里面装满了(已编译的)XAML ResourceDictionaries。 在 UserControl 中,我想将所有模板加载到 ResourceDicti
如何在用户控制库中定义 ResourceDictionary 并通过 Xaml 代码访问它们。 我创建了这样的东西:
假设我有对象: 我通常通过类似的方式引用它: 虽然这有效,但如果我第二次调用它,它会删除第一次使用并将其移至请求的第二次使用。 很明显,我调用资源的方法存在问题。如果我想要对象的唯一实例
我是一名优秀的程序员,十分优秀!