我有一个文本框,它通过 XAML 绑定到列表框,文本框也绑定到视图模型,使用多重绑定来获取它,见下文。
想法是在视图模型中使用所选项目之前必须对其进行修改。
我对这段代码不满意,对于这样一个简单的任务来说太复杂了。每个绑定都可以完美地单独工作。可以使用哪种简单的方法来选择元素修改它并使用修改后的字符串在视图模型中进行进一步处理?
--------------XAML--------------
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:TheMultiValueConverter x:Key="theConverter"/>
</Window.Resources>
<StackPanel>
<TextBox Name="textBox">
<TextBox.Text>
<MultiBinding Converter="{StaticResource theConverter}" Mode="OneWay">
<Binding/>
<Binding ElementName="textBox" Path="Text"/>
<Binding ElementName="listBox" Path="SelectedItem" Mode="OneWay"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
<ListBox Name="listBox" ItemsSource="{Binding Data}" />
</StackPanel>
</Window>
--------------视图模型--------------
public class ViewModel : INotifyPropertyChanged
{
string modified;
public string Modified
{
get { return modified; }
set
{
if (modified != value)
{
modified = value;
NotifyPropertyChanged("Modified");
}
}
}
List<string> data = new List<string> { "Test1", "Test2" };
public List<string> Data
{
get { return data; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
--------------价值转换器--------------
public class TheMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var viewModel = values[0] as ViewModel;
if (viewModel != null)
{
viewModel.Modified = (string)values[1];
if (string.IsNullOrWhiteSpace(values[1].ToString()))
return values[2];
else
return values[1];
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}