简体   繁体   English

如何使用TextBlock创建自定义文本框?

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

I want to create a custom control(or user control) which has both a textblock and a textbox as in the fig below: 我想创建一个自定义控件(或用户控件),该控件同时具有文本块和文本框,如下图所示:

在此处输入图片说明

Both the textblock and Textbox text properties are to be bound to the database fields and be able to apply styling etc. Which is the best approach for the same? textblock和Textbox文本属性都将绑定到数据库字段,并能够应用样式等。哪种方法最好?

I have come up with a solution as below: 我提出了以下解决方案:

XAML for user control: 用于用户控制的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>

Code behind for Usercontrol: 用户控件后面的代码:

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;
    }
}

XAML for actual form where Usercontrol is used: 使用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>

Code behind: 后面的代码:

 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;
    }
}

Still the binding doesnt work. 仍然绑定不起作用。 But the code : 但是代码:

textBlox1.Caption = "test"; 

is working fine. 工作正常。 where am I wrong? 我哪里错了?

用文本块和文本框创建一个用户控件,然后将值绑定到该控件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM