我在用户控件中有五个文本框控件,我想以这种方式添加依赖项属性
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 :(得分:1)
有多个解决方案:
使用属性公开完整值并使用IValueConverter
提取部分
创建五个属性,每个属性都显示完整值的一部分
两者都符合MVVM,但第二个可能通过避免过多管道而更加透明,但您可能需要更多通知(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
以更改完整字符串值,并修改TextBox
中的每个UserControl
以更改每个部分。
非常棘手,但应该做你想做的事。