【问题标题】:Bind single control to multiple properties将单个控件绑定到多个属性
【发布时间】:2016-12-21 11:05:31
【问题描述】:

我目前正在开发 WPF 表单并尝试应用 MVVM 模式。 我有一个数据网格、两个编辑控件和一个按钮。简化后的代码如下:

<syncfusion:SfDataGrid Grid.Row="1" Name="TempGrid" ItemsSource="{Binding TempProfile}" SelectedItem="{Binding SelectedTempSetpoint}">
</syncfusion:SfDataGrid>
<syncfusion:TimeSpanEdit Format="hh:mm" Value="{Binding SelectedTempSetpoint.Time, Mode=OneWay}" />
<syncfusion:UpDown Value="{Binding SelectedTempSetpoint.Value, Mode=OneWay}"  >
<Button  Content="Add" Command="{Binding AddTempSetpointCommand}">

我使用 OneWay-Binding 来更新 Edit-Control 的内容,每次或 Datagrid 中所选元素的值发生变化。但是我希望能够更改编辑控件中的值并使用该值将另一个元素添加到数据网格。为此,我想使用按钮的命令属性(以及 ViewModel 中的 DelegateCommand)。 通常,我会将编辑控件的值绑定到我的 VM 中的属性,但是如果 SelectedElement 发生更改,我将失去它们更新的功能。 这样做的正确方法是什么?使用单独的属性并在 VM 中更新它们?或者有没有办法将控件绑定到多个属性?

【问题讨论】:

    标签: wpf mvvm


    【解决方案1】:

    我能够通过 Model 和 MultiValueConverter 中的新属性来实现这一点,

    代码如下,

      <StackPanel>
        <StackPanel.Resources>
            <local:CustomMultiConverter x:Key="CustomMultiConverter"/>
        </StackPanel.Resources>
        <ListBox ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedName}" />
        <TextBox>
            <TextBox.Text>
                <MultiBinding Converter="{StaticResource CustomMultiConverter}">
                    <Binding Mode="OneWay" Path="SelectedName" />
                    <Binding Mode="TwoWay" Path="NewName" />
                </MultiBinding>
            </TextBox.Text>
        </TextBox>
        <Button Command="{Binding AddCommand}" Content="Add" />
    </StackPanel>
    
    
    
    public class ViewModel : INotifyPropertyChanged
    {
        private ICommand addCommand;
    
        public ICommand AddCommand
        {
            get { return addCommand; }
            set { addCommand = value; }
        }
    
        private ObservableCollection<string> names;
    
        public ObservableCollection<string> Names
        {
            get { return names; }
            set { names = value; }
        }
        private string selectedName;
        private string newName;
    
        public string NewName
        {
            get { return newName; }
            set { newName = value; OnPropertyChanged("NewName"); }
        }
    
        public string SelectedName
        {
            get { return selectedName; }
            set { selectedName = value;OnPropertyChanged("SelectedName"); }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public ViewModel()
        {
            Names = new ObservableCollection<string>();
            Names.Add("name1");
            Names.Add("name2");
            addCommand = new DelegateCommand(Add, CanAdd);
        }
    
        private bool CanAdd(object arg)
        {
            return true;
        }
    
        private void Add(object obj)
        {
            Names.Add(NewName);
        }
    
    
        protected virtual void OnPropertyChanged(string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    public class CustomMultiConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if(values != null)
            {
                if (values[0] != null)
                    return values[0];
            }
            return null;
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            var obj = new object[2];
            obj[0] = null;
            obj[1] = value;
            return obj;
        }
    }
    

    如有错误请指正。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 2012-03-24
      • 1970-01-01
      相关资源
      最近更新 更多