有多于一个的解决方案:
两者都是符合MVVM的,但第二个可能更透明,因为避免了太多的管道,但是您可能需要更多notifi阳离子(INotifyPropertyChanged
)电话。
编辑:全面实施
的UserControl
:
XAML:
<UserControl x:Class="WpfApplication1.SplitterControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<local:SplitConverter x:Key="splitConverter"></local:SplitConverter>
</UserControl.Resources>
<StackPanel x:Name="root" DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=MyValue,Mode=TwoWay,Converter={StaticResource splitConverter}}">
<TextBox x:Name="Textbox1" Text="{Binding [0],NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" SourceUpdated="TextBox_SourceUpdated"></TextBox>
<TextBox x:Name="Textbox2" Text="{Binding [1],NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" SourceUpdated="TextBox_SourceUpdated"></TextBox>
<TextBox x:Name="Textbox3" Text="{Binding [2],NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" SourceUpdated="TextBox_SourceUpdated"></TextBox>
<TextBox x:Name="Textbox4" Text="{Binding [3],NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" SourceUpdated="TextBox_SourceUpdated"></TextBox>
<TextBox x:Name="Textbox5" Text="{Binding [4],NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" SourceUpdated="TextBox_SourceUpdated"></TextBox>
</StackPanel>
</UserControl>
后面的代码:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WpfApplication1
{
public partial class SplitterControl : UserControl
{
public string MyValue
{
get { return (string)GetValue(MyValueProperty); }
set { SetValue(MyValueProperty, value); }
}
public static readonly DependencyProperty MyValueProperty = DependencyProperty.Register("MyValue", typeof(string), typeof(SplitterControl));
public SplitterControl()
{
InitializeComponent();
}
private void TextBox_SourceUpdated(object sender, DataTransferEventArgs e)
{
root.GetBindingExpression(DataContextProperty).UpdateSource();
}
}
}
的IValueConverter
:
using System;
using System.Globalization;
using System.Windows.Data;
namespace WpfApplication1
{
public class SplitConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value as string).Split('|');
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Join("|", value as string[]);
}
}
}
而在父母控制中,例如,在MainWindow
:
<TextBox x:Name="input" Text="First|Second|Third|Fourth|Fifth"></TextBox>
<local:SplitterControl MyValue="{Binding ElementName=input,Path=Text,Mode=TwoWay}"></local:SplitterControl>
编辑"input"
TextBox
改变完整的字符串值,并在UserControl
编辑每个TextBox
改变每一个部分。
非常棘手,但应该做你想做的。
*它不工作*是*不*有用的描述。 – Sheridan 2014-09-04 18:20:52
绑定不起作用。 – 2014-09-05 04:09:45
哇!你真的不明白,是吗?你必须帮助我们来帮助你。目前,你根本没有帮助我们,所以你不会得到任何帮助。随你便。 – Sheridan 2014-09-05 07:57:58