繁体   English   中英

如何在get属性中实现逻辑,在依赖属性WPF中进行设置?

[英]How do i implement logic in get, set in dependency property 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];
        }

    }

但这是行不通的。 我如何创建可以直接绑定到单个属性的依赖项属性。 任何帮助将不胜感激。

解决方案不止一种:

  • 使用属性公开完整值,并使用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以更改完整的字符串值,并编辑UserControl每个TextBox以更改每个部分。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM