如何使用XBox中的CheckBox,Radion Button,组合框控件绑定XML数据 [英] how to Bind the XML data with CheckBox, Radion Button, combobox controls in XAML

查看:86
本文介绍了如何使用XBox中的CheckBox,Radion Button,组合框控件绑定XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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







<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

推荐答案

您应该使用ViewModel作为XAML的DataContext。将所有控件绑定到ViewModel中的相应数据源。



步骤1:创建一个类并实现INotifyPropertyChanged

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
public class BaseViewModel : INotifyPropertyChanged
{
     public event PropertyChangedEventHandler PropertyChanged;

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





步骤2:创建对象



Step-2: Create object

public class UserType
{
    public int PKID { get; set; }
    public string Name { get; set; }
}





步骤3:创建自己的ViewModel类并实现以前的类(BaseViewModel)和您要绑定到XAML的所有其他属性。



Step-3: Create your own ViewModel class and implement previous class(BaseViewModel) and all other properties you want to bind to XAML.

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"}
        };
    }
}





步骤4:将命名空间导入xaml



Step-4: Import namespace into xaml

xmlns:vm="clr-namespace:WpfApplication1"





步骤5:使用ViewModel绑定DataContext



Step-5: Bind your DataContext with your ViewModel

<Window.DataContext>
    <vm:ItemViewModel />
</Window.DataContext>





步骤6:创建控件并绑定特定属性



Step-6: Create your control and bind with specific properties

<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" />


这篇关于如何使用XBox中的CheckBox,Radion Button,组合框控件绑定XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆