Link to home
Start Free TrialLog in
Avatar of suresh pondicherry
suresh pondicherryFlag for United States of America

asked on

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

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
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

What's not working, the dynamic XAML or the event not firing?
Avatar of suresh pondicherry

ASKER

Hi Wdosanjos,
Events are firing but values are not restored. Combobox values is not even passed to the object property.

Kind regards,
Pooja
I guess is some issue with your bindings.  Are your controls bound to Dependency Properties?  Are the bindings defined as Two-Way?
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


Hi Baskar ram and  wdosanjos,
How to bind the combo box?

Kind regards,
Pooja
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
Hi pooja,

I cant get your requirement clearly..
Could you please attach your project and explain your requirement little clearly...
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.
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
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


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
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial