-1

我有一个文本框,我想将哪个默认值绑定到组合框 selecteItem,同时我希望我的文本框绑定到 Mvvm 对象属性?我在这里检查过,但多重绑定让我感到困惑。我更愿意为这个问题提供 xaml 解决方案。

添加:

在组合框中,我将选择一个帐户,该帐户包含一些值(金额),我想显示金额,但需要将我的文本框绑定到 mvvm 模型对象元素 stAmount。因此用户可以更改组合框选择的数量,然后这个修改或未更改的数量值可以存储到文本框绑定的模型对象元素(stAmount)

4

2 回答 2

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

和xml

<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 回答
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; }
    }
}

在此示例中,我们将一个对象绑定到您ObservableCollection的对象,并跟踪通过属性选择的对象。我们将text 属性绑定到选定对象的属性。因此,当然后选择的对象发生变化时,变化中显示的值会反映.AccountComboBoxAccountSelectedItemTextBoxBalanceAccountAccountTextBoxBalanceAccount

此外,如果您更改 中的TextBoxBalance,则Account对象的值也会更新。

于 2013-09-26T09:40:04.957 回答