Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i want to bind the xml data with CheckBox, Radion Button, combobox controls in XAML.It's working fine for text box control, have issues with other controls

i don't want to keep the choices in xml/xmldataprovide, it's should be a static with xaml.  the selected items value should be updated in <MaritalStatus> node in xml data.



xmal file contenet as below


XML
<Grid 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d" Focusable="False" Height="81" Width="221">
    <Grid.Resources>
        <XmlDataProvider x:Key="ContentData" XPath="MaritalInfo"  IsInitialLoadEnabled="True" IsAsynchronous="False">
            <x:XData>
                <MaritalInfo xmlns="">
                    <MaritalStatus>Single</MaritalStatus>
                </MaritalInfo>
            </x:XData>
        </XmlDataProvider>
    </Grid.Resources>


    <Label Content="What is your marital status?"  Focusable="False" Height="36" HorizontalAlignment="Left" Margin="8,0,0,0" VerticalAlignment="Top" FontSize="16" />

     <ComboBox Name="A" DataContext="{Binding Source={StaticResource ContentData}, Mode=TwoWay,UpdateSourceTrigger=Explicit, XPath=MaritalStatus}"  
              SelectedValue="{Binding Path=MaritalStatus, Mode=TwoWay,UpdateSourceTrigger=Explicit}"  
              Height="23" HorizontalAlignment="Left"  Margin="8,32,0,0" VerticalAlignment="Top" 
              Width="171" Visibility="Visible" SelectedValuePath="Tag" IsReadOnly="False">
        <ComboBox.Resources>
            <Style TargetType="{x:Type ComboBox}">
                <Setter Property="IsDropDownOpen" Value="False"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="IsDropDownOpen" Value="True"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Resources>
        <ComboBox.Items>
            <ComboBoxItem Tag="S">Single</ComboBoxItem>
            <ComboBoxItem Tag="M">Married</ComboBoxItem>
            <ComboBoxItem Tag="D">Divorced</ComboBoxItem>
            <ComboBoxItem Tag="W">Widowed</ComboBoxItem>
            <ComboBoxItem Tag="SP">Separated</ComboBoxItem>
        </ComboBox.Items>
    </ComboBox>
   
</Grid>


Note:should not use the code behind or value converters to do this. all the stuff in xaml itself.

Is it possible?
pls help me to resolve this.

Thanks,
Vijay
Posted
Updated 17-Sep-14 5:29am
v2

1 solution

You should use ViewModel as a DataContext for the XAML. Bind all of your controls to corresponding data source from ViewModel.

Step-1: Create a class and implement INotifyPropertyChanged
C#
public class BaseViewModel : INotifyPropertyChanged
{
     public event PropertyChangedEventHandler PropertyChanged;

     public void OnPropertyChanged(string propertyName)
     {
          if (PropertyChanged != null)
               PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }
}


Step-2: Create object
C#
public class UserType
{
    public int PKID { get; set; }
    public string Name { get; set; }
}


Step-3: Create your own ViewModel class and implement previous class(BaseViewModel) and all other properties you want to bind to XAML.
C#
public class ItemViewModel : BaseViewModel
{
    public ItemViewModel()
    {
        this.LoadDatas();
    }

    private List<UserType> _userTypes;
    public List<UserType> UserTypes
    {
        get { return _userTypes; }
        set
        {
            _userTypes = value;
            base.OnPropertyChanged("UserTypes");
        }
    }

    private int _userTypeID;
    public int UserTypeID
    {
        get { return _userTypeID; }
        set
        {
            _userTypeID = value;
            base.OnPropertyChanged("UserTypeID");
        }
    }

    private void LoadDatas()
    {
        UserTypes = new List<UserType>()
        {
            new UserType { PKID=1, Name="Standard"},
            new UserType { PKID=2, Name="Premium"}
        };
    }
}


Step-4: Import namespace into xaml
C#
xmlns:vm="clr-namespace:WpfApplication1"


Step-5: Bind your DataContext with your ViewModel
C#
<Window.DataContext>
    <vm:ItemViewModel />
</Window.DataContext>


Step-6: Create your control and bind with specific properties
C#
<ComboBox Name="cboUserType" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" Width="250" Height="25" ItemsSource="{Binding UserTypes, Mode=OneWay}" SelectedValue="{Binding UserTypeID}" SelectedValuePath="PKID" DisplayMemberPath="Name" />
 
Share this answer
 
v2
Comments
Imran_Hasan 19-Sep-14 16:46pm    
Source Code => https://drive.google.com/file/d/0B9pMxdwTlVorTjFnTEZ5WjFRcTg/edit?usp=sharing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900