簡體   English   中英

如何使用TextBlock創建自定義文本框?

[英]How to create a custom Textbox with a TextBlock?

我想創建一個自定義控件(或用戶控件),該控件同時具有文本塊和文本框,如下圖所示:

在此處輸入圖片說明

textblock和Textbox文本屬性都將綁定到數據庫字段,並能夠應用樣式等。哪種方法最好?

我提出了以下解決方案:

用於用戶控制的XAML:

<UserControl x:Class="TestDependency.TextBlox"
         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"
         x:Name="abc"
         DataContext="{Binding RelativeSource={RelativeSource Self}}" >
<Grid Width="170">
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="0,1,0,0" 
               Text="{Binding Path=Caption}"
               VerticalAlignment="Top" Width="52" />
    <TextBox Height="25" HorizontalAlignment="Left" Margin="53,-2,0,0" 
             x:Name="TextBox1"
             Text="{Binding Path=Value}"
             VerticalAlignment="Top" Width="120" />
</Grid>

用戶控件后面的代碼:

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

    public static DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(TextBlox));
    public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(TextBlox));


    public string Caption
    {
        get
        {
            return (string)GetValue(CaptionProperty);
        }
        set
        {
            SetValue(CaptionProperty, value);
        }
    }


    public string Value
    {
        get
        {
            return (string)GetValue(ValueProperty);
        }
        set
        {
            SetValue(ValueProperty, value);
        }
    }

    static void ValueChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
    {
        TextBlox inst = (TextBlox)property;
        inst.TextBox1.Text = (string)args.NewValue;
    }
}

使用Usercontrol的實際形式的XAML:

 <Grid x:Name="grdmain">
    <my:TextBlox Caption="{Binding XValue, Mode=TwoWay}" Value="{Binding WindowName, Mode=TwoWay}" 
        HorizontalAlignment="Left" Margin="246,197,0,0"  x:Name="textBlox1" VerticalAlignment="Top" />
</Grid>

后面的代碼:

 public partial class MainWindow : Window
{
    DataEntities dt = new DataEntities();
    CoOrdinate oCord;
    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        oCord = dt.CoOrdinates.First();
        grdmain.DataContext = oCord;
    }
}

仍然綁定不起作用。 但是代碼:

textBlox1.Caption = "test"; 

工作正常。 我哪里錯了?

用文本塊和文本框創建一個用戶控件,然后將值綁定到該控件。

<YourUserControl TextBlockText="{Binding TextBlockText}" TextBoxText="{Binding TextBoxText, Mode=TwoWay}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM