当另一个组合框选择发生更改时,如何使用触发器执行Combobox选择chage [英] How to perform Combobox selection chage using triggers when another combobox selection change occures

查看:82
本文介绍了当另一个组合框选择发生更改时,如何使用触发器执行Combobox选择chage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在选择第一个组合框时有两个相似的组合框,然后第二个组合框想要被相同的选择。我喜欢在xaml中使用触发器

I have two similar comboboxs when selecting the first combobox then the second combobox want to get selected by the same. I like to use triggers in xaml

推荐答案

查看模型c#代码:



VIEW MODEL c# CODE :

public class AddressViewModel: INotifyPropertyChanged
   {
       private int mStreetNumber;
       public int StreetNumber {
           get
           {
               return mStreetNumber;
           }

           set
           {
               if (mStreetNumber != value)
               {
                   mStreetNumber = value;

                   // Look up street name
                   Web.AddressBookContext ctx = new Web.AddressBookContext();
                   InvokeOperation<IEnumerable<string>> op = ctx.GetStreetNames(mStreetNumber, this.GetStreetNamesCompleted, null);
               }
           }
       }

       public string StreetName { get; set; }

       private IEnumerable<string> mStreetNames;
       public IEnumerable<string> StreetNames
       {
           get
           {
               return mStreetNames;
           }
       }

       void GetStreetNamesCompleted(InvokeOperation<IEnumerable<string>> op)
       {
           mStreetNames = op.Value;
           if (PropertyChanged != null)
               PropertyChanged(this, new PropertyChangedEventArgs("StreetNames"));
       }

       public event PropertyChangedEventHandler PropertyChanged;





/ *将ViewModel绑定到XAML * /



< usercontrol x:class =StreetLookup.MainPagexmlns:x =#unknown>

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 =300d:DesignWidth =400

xmlns:sdk =http://schemas.microsoft.com/ winfx / 2006 / xaml / presentation / sdk

Loaded =UserControl_Loaded

>



< grid x:name =LayoutRootbackground =White>

< sdk:label height =27horizo​​ntalalignment =Leftmargin =23,34,0,0name =label1verticalalignment =Topwidth =76content =数字xmlns:sdk =#unknown>

< sdk:label height =28horizo​​ntalalignment =Leftmargin =178,34,0,0name =label2verticalalignment =Topwidth =120content =街道xmlns:sdk =#unknown>

< textbox height =23horizo​​ntalalignment =Leftmargin =23,67,0,0name =textBox1verticalalignment =Topwidth =120text = {Binding StreetNumber,Mode = TwoWay}>

< combobox height =23>

SelectedValue ={Binding StreetName,Mode = TwoWay}

ItemsSource ={绑定StreetNames,Mode = OneWay}

Horizo​​ntalAlignment =LeftMargin =179,66,0,0Name =comboBox1VerticalAlignment =TopWidth =162
/>







C#

private void UserControl_Loaded(object sender,RoutedEventArgs e )

{

this.DataContext = vm;

}



网络服务



amespace StreetLookup.Web

{

使用System.Collections.Generic;

使用System.ServiceModel.DomainServices.Hosting;

使用System.ServiceModel.DomainServices.Server;





// TODO:创建包含应用程序逻辑的方法。

[EnableClientAccess()]

公共类AddressBookService:DomainService

{

[Invoke]

public IEnumerable< string> GetStreetNames(int StreetNumber)

{

List< string> names = new List< string>();

if(StreetNumber == 1)

{

names.Add(Parker St);

names.Add(Zachary St);

}



if(StreetNumber == 2)

{

名字.Add(Market St);

names.Add(Ocean Ave);

}



返回姓名;

}

}

}



问候,

Praveen Nelge



/*Binding the ViewModel to the XAML*/

<usercontrol x:class="StreetLookup.MainPage" xmlns:x="#unknown">
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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
Loaded="UserControl_Loaded"
>

<grid x:name="LayoutRoot" background="White">
<sdk:label height="27" horizontalalignment="Left" margin="23,34,0,0" name="label1" verticalalignment="Top" width="76" content="Number" xmlns:sdk="#unknown">
<sdk:label height="28" horizontalalignment="Left" margin="178,34,0,0" name="label2" verticalalignment="Top" width="120" content="Street" xmlns:sdk="#unknown">
<textbox height="23" horizontalalignment="Left" margin="23,67,0,0" name="textBox1" verticalalignment="Top" width="120" text="{Binding StreetNumber,Mode=TwoWay}">
<combobox height="23">
SelectedValue="{Binding StreetName, Mode=TwoWay}"
ItemsSource="{Binding StreetNames,Mode=OneWay}"
HorizontalAlignment="Left" Margin="179,66,0,0" Name="comboBox1" VerticalAlignment="Top" Width="162"
/>



C#
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = vm;
}

Web Service

amespace StreetLookup.Web
{
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;


// TODO: Create methods containing your application logic.
[EnableClientAccess()]
public class AddressBookService : DomainService
{
[Invoke]
public IEnumerable<string> GetStreetNames(int StreetNumber)
{
List<string> names = new List<string>();
if (StreetNumber == 1)
{
names.Add("Parker St");
names.Add("Zachary St");
}

if (StreetNumber == 2)
{
names.Add("Market St");
names.Add("Ocean Ave");
}

return names;
}
}
}

Regards,
Praveen Nelge


这篇关于当另一个组合框选择发生更改时,如何使用触发器执行Combobox选择chage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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