0

次のサンプル ユーザー コントロールを作成しました。

<UserControl x:Class="DLSAdministration.Controls.CombinedTextBoxControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         d:DesignHeight="30"
         d:DesignWidth="250"
         mc:Ignorable="d">
<UserControl.Resources>
    <SolidColorBrush x:Key="BGBrush" Color="#3A3A3A" />
    <SolidColorBrush x:Key="BorderBrush" Color="#656565" />
</UserControl.Resources>
<Grid Width="{Binding Path=Width, ElementName=tb}" Height="{Binding Path=Height, ElementName=tb}">
    <TextBox Name="tb"
             Grid.Row="0"
             Grid.Column="0"
             Background="{StaticResource BGBrush}"
             BorderBrush="{StaticResource BorderBrush}"
             BorderThickness="2"
             FontFamily="Calibri"
             FontSize="16"
             Foreground="Snow">
        Content
    </TextBox>
    <TextBlock Margin="0,5,5,5"
               HorizontalAlignment="Right"
               FontStyle="Italic"
               Foreground="Red"
               Padding="2">
        Label
    </TextBlock>
</Grid>

コード ビハインド ファイルを使用する場合:

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

    // the exposed instance public properties.
    public string TextboxText
    {
        get { return (string) GetValue(TextboxTextProperty); }
        set { SetValue(TextboxTextProperty, value); }
    }

    public string TextBlockText
    {
        get { return (string)GetValue(TextBlockTextProperty); }
        set { SetValue(TextBlockTextProperty, value); }
    }

    private static readonly DependencyProperty TextboxTextProperty =
        DependencyProperty.Register
            (
                "TextboxText",
                typeof (string),
                typeof (CombinedTextBoxControl),
                new FrameworkPropertyMetadata("TextboxText")
            );

    private static readonly DependencyProperty TextBlockTextProperty =
        DependencyProperty.Register
            (
                "TextBlockText",
                typeof(string),
                typeof(CombinedTextBoxControl),
                new FrameworkPropertyMetadata("TextBlockText ")
            );
}

私の UserControl は次のように消費されます。

        <my:CombinedTextBoxControl x:Name="combinedTextBoxControl1"
                               Grid.Column="1"
                               Width="182"
                               Margin="77,76,0,0"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Top"
                               TextBlockText="Name"
                               TextboxText="Ibrar Mumtaz" />

このコントロールにデータをバインドしていませんし、xaml 経由で提供したサンプル データを表示することさえできませんか? あなたは間違いを見つけることができますか?私はSOを見回して、他の人の間違いに気づきましたが、明らかな間違いを犯していないので、これに完全に困惑しています....:S

4

1 に答える 1

1

どのように機能すると思いますか? CombinedTextBoxControl に依存関係プロパティがありますが、どこでも使用していません。このプロパティをコントロール テンプレート内の子コントロールに割り当てたい場合は、次のようにします。

ユーザーコントロールに名前を付けます

         <UserControl x:Class="DLSAdministration.Controls.CombinedTextBoxControl" 
            x:Name="myControl"

次のようにテキストボックスとテキストブロックにバインディングを追加します

Text="{Binding ElementName=myControl, TextBlockText}"

別のメモ: レイアウトが間違っています。セルが 1 つだけのグリッドがあり、その中にテキストブロックとテキストボックスを配置すると、一方が他方の上に配置されます。ラベル用に 1 つ、テキストボックス用に 1 つ、2 つの ColumnDefinitions を追加して、両方のコントロールに適切な添付プロパティを設定してみてください (テキストブロックでは Grid.Column="0"、テキストボックスでは Grid.Column="1")。

于 2012-05-21T10:52:18.740 に答える