-1

テキスト ボックスがあり、どのデフォルト値をコンボ ボックス selecteItem にバインドしたいのですか?同時に、テキスト ボックスを Mvvm オブジェクト プロパティにバインドしたいですか? 私はここでチェックしましたが、マルチバインディングは私を混乱させます。この問題には xaml ソリューションが必要です。

添加:

コンボボックスでアカウントを選択します。そのアカウントにはいくつかの値 (Amount) が含まれています。Amount を表示したいのですが、テキスト ボックスを mvvm モデル オブジェクト要素 stAmount にバインドする必要があります。したがって、ユーザーはコンボボックスによって選択された量を変更でき、この変更された、または変更されていない量の値は、テキスト ボックスにバインドされたモデル オブジェクト要素 (stAmount) に格納できます。

4

2 に答える 2

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ものを追跡します。text プロパティを選択したオブジェクトのプロパティにバインドします。したがって、選択されたオブジェクトが に表示される値を変更すると、のが反映されます。AccountSelectedItemTextBoxBalanceAccountAccountTextBoxBalanceAccount

さらに、 の値を変更すると、オブジェクトTextBoxBalanceAccountが更新されます。

于 2013-09-26T09:40:04.957 に答える
0

コンボボックスではなく、ビューモデルで選択した値のプロパティにテキストボックスをバインドしたいようです。

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>
于 2013-09-26T08:42:27.477 に答える