2013-09-26 47 views
-1

我有一個文本框,我想綁定到組合框selecteItem的默認值,同時我希望我的文本框綁定到Mvvm對象屬性? 我在這裏查過,但多重綁定混淆了我。 我寧願爲此問題提供xaml解決方案。將文本框綁定到兩個源WPF

增加:

在組合框,我會選擇一個帳戶,該帳戶包含一些值(金額),我想顯示金額,但需要我的文本框中有界到MVVM模型對象元素stAmount。所以用戶可以更改組合框選擇的金額,然後這個修改或不變的金額值可以存儲到文本框綁定模型對象元素(stAmount)

+0

您一次只能顯示一個值,不論是selecteditem還是VM屬性...或者您想要根據兩者來計算文本框文本的值? – Nitin

+0

請澄清你正在嘗試做什麼,沒有辦法在當前狀態下回答你的問題 –

+0

你可能想看看[PriorityBinding](http://msdn.microsoft.com/en-us/library /system.windows.data.prioritybinding.aspx)。 – Clemens

回答

0

利用的INotifyPropertyChanged

XAML

<Window x:Class="INotifyPropertyChangedExample.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}" 
     Title="INotifyPropertyChanged Example" Width="380" Height="100"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="150" /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
     <Label Content="Account Name:" /> 
     <Label Grid.Row="1" Grid.Column="0" Content="Account Balance:" /> 
     <ComboBox Grid.Row="0" Grid.Column="1" Width="200" Height="25" ItemsSource="{Binding AccountsCollection}" SelectedItem="{Binding SelectedAccount}" DisplayMemberPath="Name" /> 
     <TextBox Grid.Column="1" Grid.Row="1" Width="200" Height="25" Text="{Binding SelectedAccount.Balance}" /> 
    </Grid> 
</Window> 

C#

namespace INotifyPropertyChangedExample 
{ 
    using System.Collections.ObjectModel; 
    using System.ComponentModel; 
    using System.Windows; 

    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
     private ObservableCollection<Account> acctountsCollection; 
     public ObservableCollection<Account> AccountsCollection 
     { 
      get 
      { 
       return this.acctountsCollection; 
      } 
      set 
      { 
       this.acctountsCollection = value; 
       OnPropertyChanged(); 
      } 
     } 

     private Account selectedAccount; 
     public Account SelectedAccount 
     { 
      get 
      { 
       return this.selectedAccount; 
      } 
      set 
      { 
       this.selectedAccount = value; 
       OnPropertyChanged(); 
      } 
     } 

     public MainWindow() 
     { 
      InitializeComponent(); 
      this.AccountsCollection = new ObservableCollection<Account>() 
      { 
       new Account { Id = 1, Name = "My super account", Balance = 123.45 }, 
       new Account { Id = 2, Name = "My super account 2", Balance = 543.21 }, 
      }; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected virtual void OnPropertyChanged(string propertyName = null) 
     { 
      var handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 

    public class Account 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public double Balance { get; set; } 
    } 
} 

在此示例中,我們將ObservableCollectionAccount對象與您的ComboBox綁定,並通過SelectedItem屬性跟蹤哪個Account被選中。我們將TextBox文本屬性綁定到所選Account對象的Balance屬性。因此,當選擇Account對象時,TextBox中顯示的值會發生變化,以反映AccountBalance

此外,如果更改TextBox中的值,則會更新Account對象的Balance值。

+0

非常感謝你,它的作品像一個魅力。 –

0

在我看來,你想你的文本框綁定到選定的值在您的viewmodel屬性不是組合框。

using System.Collections.ObjectModel; 
using System.Windows; 

namespace WpfApplication1 
{ 
    public partial class MainWindow : Window 
    { 
     public ObservableCollection<string> Items 
     { 
      get { return (ObservableCollection<string>)GetValue(ItemsProperty); } 
      set { SetValue(ItemsProperty, value); } 
     } 

     public static readonly DependencyProperty ItemsProperty = 
      DependencyProperty.Register("Items", typeof(ObservableCollection<string>), typeof(MainWindow), new PropertyMetadata(null)); 


     public string SelectedValue 
     { 
      get { return (string)GetValue(SelectedValueProperty); } 
      set { SetValue(SelectedValueProperty, value); } 
     } 

     public static readonly DependencyProperty SelectedValueProperty = 
      DependencyProperty.Register("SelectedValue", typeof(string), typeof(MainWindow), new PropertyMetadata(null)); 


     public MainWindow() 
     { 
      InitializeComponent(); 
      Items = new ObservableCollection<string>(); 
      Items.Add("Value 1"); 
      Items.Add("Value 2"); 
      Items.Add("Value 3"); 
      Items.Add("Value 4"); 
      Items.Add("Value 5"); 
      Items.Add("Value 6"); 
     } 
    } 
} 

和XAML

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid > 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 

     <ComboBox Grid.Row="0" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedValue}"/> 
     <TextBox Grid.Row="1" Text="{Binding SelectedValue}"/> 
    </Grid> 
</Window> 
+0

非常感謝你,Veleous和你的答案都是一樣的。非常感謝。 –