-1

4 つのテキスト ボックス [tb1、tb2、tb3、および tb4] を含むユーザー コントロール [UC1 など] が必要です。このユーザー コントロールには、これらのテキスト ボックスにバインドする 4 つの通常のプロパティ [prop1、prop2、prop3、prop4 など] が必要です。このユーザー コントロールによって外部の世界に公開される依存関係プロパティ [dp など] が必要です。このユーザー コントロールは、クラス [C1 など] のプロパティ [StrProp など] から 1 つの文字列 [0\abc|1\def|2\ghi|3\jkl など] を取得し、4 つの部分 [abc、def、abc、def、 ghi、および jkl] を使用して、ユーザー コントロールの 4 つのテキスト ボックスに表示します。テキストボックスの一部またはすべてでユーザーが何らかの変更を行った場合、変更されたすべてのテキストが結合され、クラス C1\StrProp プロパティに反映される必要があります。また、私の要件は、UI\XAML で dp を StrProp にバインドする必要があることです。検証も適切に行う必要があります。

誰かが例を書いて私を助けてくれますか? サンプル クラスは次のとおりです。

MyMultiTextBoxUserControl.xaml

<UserControl x:Class="MyMultiTextBoxControl_UsingNConsuming.MyMultiTextBoxUserControl"
             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" 
             mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height=".25*"/>
            <RowDefinition Height=".25*"/>
            <RowDefinition Height=".25*"/>
            <RowDefinition Height=".25*"/>
        </Grid.RowDefinitions>
        <TextBox Grid.Row="0" Text="{Binding ElementName=UserControl, Path=CombinedField1 }"/>
        <TextBox Grid.Row="1" Text="{Binding ElementName=UserControl, Path=CombinedField2}"/>
        <TextBox Grid.Row="2" Text="{Binding ElementName=UserControl, Path=CombinedField3}"/>
        <TextBox Grid.Row="3" Text="{Binding ElementName=UserControl, Path=CombinedField4}"/>
    </Grid>
</UserControl>

MyMultiTextBoxUserControl.xaml.cs

public partial class MyMultiTextBoxUserControl : UserControl
    {
        public MyMultiTextBoxUserControl()
        {
            InitializeComponent();
        }

        //static FrameworkPropertyMetadata propertydata = new FrameworkPropertyMetadata("Hello",
        //    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(PropertyChanged_Callback), new CoerceValueCallback(CoerceValue_Callback),
        //    false, UpdateSourceTrigger.LostFocus);

        //public static readonly DependencyProperty CombinedTextProperty =
        //    DependencyProperty.Register("CombinedText", typeof(string), typeof(MyMultiTextBoxUserControl), propertydata, new ValidateValueCallback(Validate_ValueCallback));


        static FrameworkPropertyMetadata propertydata = new FrameworkPropertyMetadata("Hello",
           FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(PropertyChanged_Callback));
        public static readonly DependencyProperty CombinedTextProperty =
            DependencyProperty.Register("CombinedText", typeof(string), typeof(MyMultiTextBoxUserControl), propertydata);


        private static bool Validate_ValueCallback(object value)
        {
            string str=value as string;
            bool result = true;
            if (str.Length > 28)
                result = false;
            if (str.Length < 1)
                result = false;
            if (str.Substring(0, 2) != "0'\'")
                result = false;
            if (str.Contains("1'\'") == false || str.Contains("2'\'") || str.Contains("3'\'"))
                result = false;
            return result;
        }

        private static object CoerceValue_Callback(DependencyObject obj,object value)
        {
            return value;
        }

        private static void PropertyChanged_Callback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {            
            MyMultiTextBoxUserControl control=(MyMultiTextBoxUserControl)obj;
            string select = e.NewValue.ToString();
            char[] pipeDelim,slashDelim;
            string[] pipeSplt;
            pipeDelim = new char[] { '|' };
            slashDelim = new Char[] { '/' };
            pipeSplt = select.Split(pipeDelim);
            if (pipeSplt.Length == 1)
                return;
            string[][] str = new string[4][];
            int x = 0;
            foreach (string s in pipeSplt)
            {
                if (string.IsNullOrEmpty(s) == false)
                {
                    str[x] = s.Split(slashDelim);
                    x++;
                }
            }
            control.CombinedField1 = str[0][1];
            control.CombinedField2 = str[1][1];
            control.CombinedField3 = str[2][1];
            control.CombinedField4 = str[3][1];
        }

        public string CombinedText
        {
            get { return GetValue(CombinedTextProperty) as string; }
            set { SetValue(CombinedTextProperty, value); }
        }

        public string CombinedField1 
        {
            get;  set; 
        }

        public string CombinedField2
        {
            get;
            set;
        }

        public string CombinedField3
        {
            get;
            set;
        }

        public string CombinedField4
        {
            get;
            set;
        }
    }

CombinedStringClass.cs

namespace MyMultiTextBoxControl_UsingNConsuming
{
    public class CombinedStringClass
    {
        public CombinedStringClass() { }
        string m_CombinedString;
        public string CombinedString
        {
            get { return m_CombinedString; }
            set
            {
                if (m_CombinedString != value)
                    m_CombinedString = value;
            }
        }
    }
}

ConsumerClass.xaml

<Window x:Class="MyMultiTextBoxControl_UsingNConsuming.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyMultiTextBoxControl_UsingNConsuming;assembly="        
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:CombinedStringClass x:Key="myClass"/>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.5*"/>
            <ColumnDefinition Width="0.5*"/>
        </Grid.ColumnDefinitions>        
        <Grid.RowDefinitions>
            <RowDefinition Height="0.33*"/>
            <RowDefinition Height="0.34*"/>
            <RowDefinition Height="0.33*"/>
        </Grid.RowDefinitions>
        <TextBlock Text="User Control Text Boxes" Grid.Row="0" Grid.Column="0" Foreground="Black" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <local:MyMultiTextBoxUserControl Grid.Row="0" Grid.Column="1" Foreground="Black" CombinedText="{Binding Source=myClass, Path=CombinedString, Mode=TwoWay,FallbackValue=DataNotBound}"/>
        <TextBlock Text="Combied String" Grid.Row="2" Grid.Column="0" Foreground="Black" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBox Name="OneStringTextBox" Grid.Row="2" Grid.Column="1" Foreground="Black" Text="0\abc|1\def|2\ghi|3\jkl" IsEnabled="False"/>
    </Grid>
</Window>

また、OneStringTextBox に反映される [0\f|1\gh|2\zx|3\oo] の形式になるように、UserControl のテキスト ボックスの変更されたテキストを結合する必要があります。また、文字列の合計の長さは 28 で、各テキスト ボックスの最大長は 7 です。

4

1 に答える 1

0

第18章を読んWPF in C# 2010: Windows Presentation Foundation in .NET 4 Matthew MacDonaldでください。あなたを助ける素晴らしい例があります。

ユーザー コントロールに名前を付け、{Binding ElementName=UserControl... を {Binding ElementName=NameOfUserControl に置き換え、CombinedFields プロパティを DP に変換します。

于 2013-01-08T19:36:29.330 に答える