简体   繁体   English

如何直接绑定XAML UWP中的属性

[英]How to bind directly properties in xaml uwp

 public sealed partial class MainPage : Page
    {
        //public static dynamic resource;

        public  ResourceModel vm = new ResourceModel();        

        public MainPage()
        {
            this.InitializeComponent();              
        }
     }


public class ResourceModel 
    {
        private Dictionary<string, string> _resource;
        public Dictionary<string, string> Resource
        {
            get { return _resource; }
            set
            {
               _resource=Value;
            }
        }
    }

Xaml Code Xaml代码

<TextBlock Text="{Binding vm.Resource[Account] ,Mode=TwoWay}" FontSize="15" Margin="10 0 30 0" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Center" Grid.Row="1" Grid.Column="0"></TextBlock>

Description I want to bind my textblock from the ResourceModel without DataContext and Name of textblock. 描述我想从ResourceModel绑定我的文本块,而没有DataContext和文本块的名称。 Only i need binding using property Name. 只有我需要使用属性名称进行绑定。

There are a few places which you're missing out. 您会错过一些地方。 Below is some information on em: 以下是有关em的一些信息:

  1. The Binding is searching for a data context which is not available. Binding正在搜索不可用的数据上下文。 In case you don't have a viewmodel and you want to use the legacy binding to bind UI elements to the code behind you need to add DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}" in your <Page> tag of xaml . 如果没有viewmodel并且想使用旧式binding将UI元素binding到后面的代码,则需要在<Page>标签中添加DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}" xaml This will tell the Binding Engine that you're binding to the code behind. 这将告诉绑定引擎您正在绑定到后面的代码。
  2. Also, you can't bind to fields, you can only bind to properties as fields don't have a get and set . 另外,您不能绑定到字段,只能绑定到properties因为字段没有getset So in your code behind your vm is a field and not a property. 因此,在您的vm后面的代码中,是字段而不是属性。 Change it to public ResourceModel vm { get; set; } = new ResourceModel(); 将其更改为public ResourceModel vm { get; set; } = new ResourceModel(); public ResourceModel vm { get; set; } = new ResourceModel(); and it'll start working. 它将开始工作。

That being said, there are 3 ways you can go about it: 话虽这么说,您可以通过3种方式进行处理:

1. using binding to codebehind: 1.使用binding到代码隐藏:

In the <page> tag add the DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}" for telling the binding engine that you'll be binding to code behind. <page>标记中添加DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"以告诉绑定引擎您将要绑定到后面的代码。

Your textblock code looks like below: 您的textblock代码如下所示:

<TextBlock Text="{Binding vm.Resource[Account]}"/>

I don't understand why is the Mode set to two way as it's a Textblock and is un-editable and legacy binding is by default oneWay . 我不明白为什么将Mode设置为two way因为它是Textblock且不可编辑,并且默认情况下,legacy bindingoneWay

2. using binding with ResourceModel 2.与ResourceModel binding使用

Another way would be to set the data context of the textblock to directly bind to the Model. 另一种方法是将textblock的数据上下文设置为直接绑定到模型。 below is the code: 下面是代码:

 <TextBlock Text="{Binding Resource[Account]}">
       <TextBlock.DataContext>
            <local:ResourceModel/>
       </TextBlock.DataContext>
  </TextBlock>

3. use x:bind for code behind binding 3.使用x:bind的代码

The x:bind by default binds to the code behind so you can use x:bind instead of binding. 默认情况下, x:bind绑定到后面的代码,因此您可以使用x:bind代替绑定。 The issue you face with x:bind is because you can't set in the key in xaml when using x:bind. x:bind面临的问题是因为使用x:bind时无法在xaml中设置密钥。 Use converters and then pass in your key as a converter parameter and it'll all start to work just fine. 使用转换器,然后将您的密钥作为转换器参数传递,一切都将开始正常工作。 if you do plan on using it below is the xaml code: 如果您打算使用它,则下面是xaml代码:

<TextBlock Text="{x:Bind vm.Resource,Converter={StaticResource DictionaryValueFetcher},ConverterParameter='account'}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM