1

さまざまな事業体(顧客など)のマスターデータを入力するためのUIを作成しています。TextBlock と TextBox を頻繁にグループ化する必要があります。すなわち

<Label Content="Civil Status:" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="3" Name="civilStatusTextBox" Text="{Binding Path=CivilStatus, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
<Label Content="Company:" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="3" Name="companyTextBox" Text="{Binding Path=Company, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />

タイピングを減らしてこれを行う方法はありますか?すなわち

<custom:LabeledTextBox Label="Civil Status:" Text="{Binding Path=CivilStatus, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" ... />

または、このようなものを提供するライブラリはありますか?

EDIT : コンテナーGridをしばらく忘れて、それが StackPanel であると想定します。

4

2 に答える 2

2

これが私がなんとかまとめた段階的な解決策です。準備として、次の内容を持つ非常に単純な UserControlがあると仮定します。XAML

<UserControl x:Class="WpfApplication2.UserControl1" [ ... auto gen code removed ... ] >
     <TextBox MinWidth="50" x:Name="TBox" />        
</UserControl>

UserControl を使用するから、基本的に のプロパティXAMLのデータ バインディングを設定する必要があります。理想的には、次のような単純な構文を使用できます。TextTBox

<local:UserControl1 TBox.Text="{Binding ...}" />

残念ながら、サブ要素のプロパティをターゲットにすることを可能にする XAML 構文はわかりません。そのため、次善の策は、UserControl 自体に「ステージング」プロパティを導入し、それをバインドすることです。

Binding は Dependency プロパティに対してのみ機能するため、導入するプロパティはDependencyProperty. Textまた、プロパティをTBoxコードから直接 DependencyPropertyにバインドします。

UserControl のコード ビハインドは次のようになります。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();

            // Set binding from code
            this.TBox.DataContext = this;
            this.TBox.SetBinding(TextBox.TextProperty, new Binding { Path = new PropertyPath("TBValue"), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
        }

        public static readonly DependencyProperty TBValueProperty = DependencyProperty.Register("TBValue", typeof(string), typeof(UserControl1));

        public string TBValue
        {
            get { return this.GetValue(TBValueProperty) as string; }
            set { this.SetValue(TBValueProperty, value); }
        }
    }
}

これを使用すると、次のように UserControl を使用して、TBValueプロパティにバインドできます。

<local:UserControl1 TBValue="{Binding Path=Test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
于 2013-03-08T08:32:29.080 に答える
0

達成したいこと (Master-Detail-Views) は、Visual Studio によってすぐに使用できるように実際に十分にサポートされています。次のメニュー構造Project -> Add Data Sourceを開きます: データ ソース タイプを選択しますObject。以下で、入力フィールドを生成するクラスを選択し、ウィザードを終了します。

ここに画像の説明を入力

次に、まだ開いていない場合は、データ ソース ツール ウィンドウを開きます(Shift+Alt+D)。生成したタイプの DataSource が表示されます。オブジェクトの各プロパティのラベル付きフィールドを取得するには、ソース ドロップダウンを開いて をクリックしますDetails

ここに画像の説明を入力

プロパティにもこのようなドロップダウンがあり、編集方法を選択できることに注意してください (ComboBox、TextBox、Custom、エディタなしなど)。DataSource をウィンドウにドラッグするだけです。必要なすべてのラベルとエディターで満たされたグリッドが得られます。DataBinding と検証もすぐにサポートされるため、生成された Grid を設定するだけで済みますDataContext

これで手間が省けることを願っています。

PSスクリーンショットはドイツのVSインスタンスで作成されていますが、正しいダイアログ/ウィンドウを特定するのに役立つと思いました.

于 2013-03-08T07:03:33.823 に答える