gpt4 book ai didi

wpf - 将单个控件绑定(bind)到多个属性

转载 作者:行者123 更新时间:2023-12-03 10:43:30 30 4
gpt4 key购买 nike

我目前正在开发 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 中选定元素的值发生更改。但是我希望能够更改编辑控件中的值并使用该值将另一个元素添加到数据网格中。为此,我想使用 Button 的 Command 属性(以及 ViewModel 中的 DelegateCommand)。
通常,我会将编辑控件的值绑定(bind)到我的 VM 中的属性,但是如果 SelectedElement 发生更改,我将失去它们更新的功能。
这样做的正确方法是什么?使用单独的属性并在 VM 中更新它们?或者有没有办法将控件绑定(bind)到多个属性?

最佳答案

我能够通过 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;
}
}

如果有任何问题,请纠正我。

关于wpf - 将单个控件绑定(bind)到多个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41261311/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com