Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

store xaml information in a object and load the values from the object to the respective xaml pages if revisited

Posted on 2011-02-10
12
393 Views
Last Modified: 2013-11-12
Hi Experts,
I have combobox, text boxes, check boxes and radio button in xaml page. I need to store the values in a object so that when i revisit the respective xaml page, could reload the values from the object to xaml fields.

I was using PropertyChangedEventArgs and it is not working.
I can provide more information if it is not sufficient

Kind regards,
Pooja
0
Comment
Question by:poojasureshkumar
  • 5
  • 5
  • 2
12 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34866189
What's not working, the dynamic XAML or the event not firing?
0
 

Author Comment

by:poojasureshkumar
ID: 34866253
Hi Wdosanjos,
Events are firing but values are not restored. Combobox values is not even passed to the object property.

Kind regards,
Pooja
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34866410
I guess is some issue with your bindings.  Are your controls bound to Dependency Properties?  Are the bindings defined as Two-Way?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 2

Expert Comment

by:baskar_ram
ID: 34869900
Try to set the DataContext of the Xaml as below. User your object instead of MyObject.

 public partial class MainPage : UserControl
    {
        MyObject myobject = new MyObject();

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {           
            this.DataContext = myobject;
        }
    }

Open in new window


0
 

Author Comment

by:poojasureshkumar
ID: 34871633
Hi Baskar ram and  wdosanjos,
How to bind the combo box?

Kind regards,
Pooja
0
 

Author Comment

by:poojasureshkumar
ID: 34872019
Hi wdosanjos and Baskar ram,
have 3 buttons in xaml page. Each button display respective xaml page with unique set of controls.
Consider - button1 shows Xaml1 with ControlArray1, Button2 shows Xaml2 with ControlArray2.

Action:
=======
When i click button1 after filling controlArray1 (fields in xaml1) should display Xaml2.
Now let me fill these fields in xaml2 and when i go back to xaml1, i should see my values..This is my problem.
My another problem is to deal with binding the combo boxes in the pages.

Advance thanks for the great help.

Kind regards,
Pooja
0
 
LVL 2

Expert Comment

by:baskar_ram
ID: 34885643
Hi pooja,

I cant get your requirement clearly..
Could you please attach your project and explain your requirement little clearly...
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34887590
Below is a sample code of a Combo box binding.  It contains a combo box and a couple of labels to display the selected value.

MainPage.xaml
<UserControl x:Class="ComboboxSample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input">

    <Grid x:Name="LayoutRoot" Background="White">
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="188,170,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120"
                        ItemsSource="{Binding ComboBoxList}" 
                        DisplayMemberPath="Description"
                        SelectedItem="{Binding TheSelectedItem, Mode=TwoWay}">
        </ComboBox>
        <dataInput:Label Height="20" HorizontalAlignment="Left" Margin="160,211,0,0" Name="label1" VerticalAlignment="Top" Width="22" Content="{Binding TheSelectedItem.Value, Mode=TwoWay}" />
        <dataInput:Label Height="20" HorizontalAlignment="Left" Margin="188,211,0,0" Name="label2" VerticalAlignment="Top" Width="120" Content="{Binding TheSelectedItem.Description, Mode=TwoWay}" />
        <dataInput:Label Height="20" HorizontalAlignment="Left" Margin="95,211,0,0" Name="label3" VerticalAlignment="Top" Width="59" Content="Selected:"/>
    </Grid>
</UserControl>

Open in new window


MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ComboboxSample
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            ComboBoxList = new List<MyOption>()
            {
                new MyOption(){Value=1,Description="Value 1"},
                new MyOption(){Value=2,Description="Value 2"},
                new MyOption(){Value=3,Description="Value 3"},
                new MyOption(){Value=4,Description="Value 4"},
                new MyOption(){Value=5,Description="Value 5"} 
            };

            TheSelectedItem = ComboBoxList[2];

            this.DataContext = this;
        }

        public List<MyOption> ComboBoxList { get; set; }

        public MyOption TheSelectedItem
        {
            get { return (MyOption)GetValue(TheSelectedItemProperty); }
            set { SetValue(TheSelectedItemProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TheSelectedItem.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TheSelectedItemProperty =
            DependencyProperty.Register("TheSelectedItem", typeof(MyOption), typeof(MainPage), new PropertyMetadata(null));
    }

    public class MyOption
    {
        public int Value { get; set; }
        public string Description { get; set; }
    }
}

Open in new window


I hope this helps.

Let me know if you have further questions.
0
 

Author Comment

by:poojasureshkumar
ID: 34888246
Hi wdosanjos and Baskar Ram,
I need the code in VB.NET. Please find the binding code for textbox

<TextBox x:Name="txtAge" Text="{Binding Age, Mode=TwoWay}" Grid.Row="7" Grid.Column="3" Height="20" Margin="173,4,178,1" Grid.ColumnSpan="3"></TextBox>

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _race As String

Public Property Age() As String
        Get
            Return _age
        End Get
        Set(ByVal value As String)
            _age= value
          ObjectStore.Age= _age
            OnPropertyChanged(New PropertyChangedEventArgs("Age"))
        End Set
    End Property

I am storing the values in the xaml in this ObjectStore.

Similarly i need to bind the selected item of combo to this object store..

Kind regards,
Pooja
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34888530
Here is the equivalent VB code:

MainPage.xaml.vb
Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()

        ComboBoxList = New List(Of MyOption)
        ComboBoxList.Add(New MyOption(1, "Option 1"))
        ComboBoxList.Add(New MyOption(2, "Option 2"))
        ComboBoxList.Add(New MyOption(3, "Option 3"))
        ComboBoxList.Add(New MyOption(4, "Option 4"))
        ComboBoxList.Add(New MyOption(5, "Option 5"))

        TheSelectedItem = ComboBoxList(2)

        Me.DataContext = Me

    End Sub


    Private _comboBoxList As List(Of MyOption)
    Public Property ComboBoxList() As List(Of MyOption)
        Get
            Return _comboBoxList
        End Get
        Set(ByVal value As List(Of MyOption))
            _comboBoxList = value
        End Set
    End Property

    Public Shared ReadOnly _theSelectedItem As DependencyProperty _
        = DependencyProperty.Register("TheSelectedItem", GetType(MyOption), GetType(MainPage), New PropertyMetadata(Nothing))

    Public Property TheSelectedItem() As MyOption
        Get
            Return DirectCast(GetValue(_theSelectedItem), MyOption)
        End Get
        Set(ByVal value As MyOption)
            SetValue(_theSelectedItem, value)
        End Set
    End Property


End Class

Public Class MyOption

    Public Sub New(ByVal v As Integer, ByVal d As String)
        _id = v
        _description = d
    End Sub

    Private _id As Integer
    Public Property Value() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property


    Private _description As String
    Public Property Description() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
        End Set
    End Property


End Class

Open in new window


MainPage.xaml
<UserControl x:Class="ComboboxSample2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input">

    <Grid x:Name="LayoutRoot" Background="White">
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="188,170,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120"
                        ItemsSource="{Binding ComboBoxList}" 
                        DisplayMemberPath="Description"
                        SelectedItem="{Binding TheSelectedItem, Mode=TwoWay}">
        </ComboBox>
        <dataInput:Label Height="20" HorizontalAlignment="Left" Margin="160,211,0,0" Name="label1" VerticalAlignment="Top" Width="22" Content="{Binding TheSelectedItem.Value, Mode=TwoWay}" />
        <dataInput:Label Height="20" HorizontalAlignment="Left" Margin="188,211,0,0" Name="label2" VerticalAlignment="Top" Width="120" Content="{Binding TheSelectedItem.Description, Mode=TwoWay}" />
        <dataInput:Label Height="20" HorizontalAlignment="Left" Margin="95,211,0,0" Name="label3" VerticalAlignment="Top" Width="59" Content="Selected:"/>
    </Grid>
</UserControl>

Open in new window


I suggest changing you 'Age' property to be a DependencyProperty as follows:

    Public Shared ReadOnly _age As DependencyProperty _
        = DependencyProperty.Register("Age", GetType(String), GetType(<<your page class name>>), New PropertyMetadata(Nothing))

    Public Property Age() As String
        Get
            Return DirectCast(GetValue(_age), String)
        End Get
        Set(ByVal value As String)
            SetValue(_age, value)
        End Set
    End Property

Open in new window


0
 

Author Comment

by:poojasureshkumar
ID: 34889224
Hi wdosanjos,

<ComboBox x:Name="ddAge" Width="148" Height="20"  Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="3" Margin="111,3,43,2" SelectedItem="{Binding Age, Mode=TwoWay}">
<ComboBoxItem Content="21 to 30" />
            <ComboBoxItem Content="31 to 40" />
<ComboBoxItem Content="41 to 50" />
            <ComboBoxItem Content="51 to 60" />

        </ComboBox>
       

Test.VB
======
Public Class Test
    Implements INotifyPropertyChanged

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private _age As String

 Public Shared ReadOnly _ageAs DependencyProperty _
        = DependencyProperty.Register("Age", GetType(String), GetType(Test), New PropertyMetadata(Nothing))


Public Property Age() As String
        Get
            Return DirectCast(GetValue(_age), String)
        End Get
        Set(ByVal value As String)
            SetValue(_age, value)
        End Set
    End Property

I am getting error in GetValue and SetValue(GEtValue is not declared). What i am missing here.


I need to get the selected item in Test.Age().

Kind regards,
Pooja
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 34898814
Change your Test class definition as follows:

Public Class Test
    Inherits DependencyObject
    Implements INotifyPropertyChanged



0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question