umasankar
umasankar

Reputation: 607

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

<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. Only i need binding using property Name.

Upvotes: 0

Views: 1258

Answers (1)

iam.Carrot
iam.Carrot

Reputation: 5276

There are a few places which you're missing out. Below is some information on em:

  1. The Binding is searching for a data context which is not available. 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. 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. So in your code behind your vm is a field and not a property. Change it to public ResourceModel vm { get; set; } = new ResourceModel(); and it'll start working.

That being said, there are 3 ways you can go about it:

1. using binding to codebehind:

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.

Your textblock code looks like below:

<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.

2. using binding with ResourceModel

Another way would be to set the data context of the textblock to directly bind to the Model. below is the code:

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

3. use x:bind for code behind binding

The x:bind by default binds to the code behind so you can use x:bind instead of binding. The issue you face with x:bind is because you can't set in the key in xaml when using x:bind. 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:

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

Upvotes: 1

Related Questions