2014-09-04 26 views
1

我有一个用户控制的五个TextBox控件,我想以这样的方式我如何实现get,在依赖项属性WPF中设置逻辑?

public string MyValue 
    { 
     get 
     { 
      return Textbox1.Text.Trim() + "." + Textbox2.Text.Trim() + "." + Textbox3.Text.Trim() + "." + Textbox4.Text.Trim() + "|" + Textbox5.Text.Trim(); 
     } 
     set 
     { 

      Textbox1.Text = value.Split('|')[0]; 
      Textbox2.Text = value.Split('|')[1]; 
      Textbox3.Text = value.Split('|')[2]; 
      Textbox4.Text = value.Split('|')[3]; 
      Textbox5.Text = value.Split('|')[4]; 
     } 

    } 

添加一个依赖属性,但它无法正常工作。我如何创建一个可直接绑定到单个属性的依赖项属性。任何帮助将不胜感激。

+0

*它不工作*是*不*有用的描述。 – Sheridan 2014-09-04 18:20:52

+0

绑定不起作用。 – 2014-09-05 04:09:45

+0

哇!你真的不明白,是吗?你必须帮助我们来帮助你。目前,你根本没有帮助我们,所以你不会得到任何帮助。随你便。 – Sheridan 2014-09-05 07:57:58

回答

1

有多于一个的解决方案:

  • 暴露与属性的全部价值并使用IValueConverter提取部件

  • 创建五个属性,每个曝光全值的一部分

两者都是符合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改变每一个部分。

非常棘手,但应该做你想做的。

+0

由于它是一个用户控件,我想将它作为一个单独的属性公开。当我设置它的值应该出现在不同的文本框,当我得到它应该连接值。 (如上) – 2014-09-05 04:12:17

+0

@ user1487911我已经更新了我的答案,完整的实现... – Pragmateek 2014-09-05 14:15:54