簡體   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