小编Sha*_*awn的帖子

WPF自定义文本框控件未正确绑定文本

我有一个自定义TextBox控件.我试图将它绑定到对象的简单描述字符串属性.如何让绑定工作?如果我将其更改为TextBox,相同的绑定工作正常.

<Style x:Key="{x:Type TaskDash:TextBoxWithDescription}" TargetType="{x:Type TaskDash:TextBoxWithDescription}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TaskDash:TextBoxWithDescription}">

                        <TextBox>
                        </TextBox>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
Run Code Online (Sandbox Code Playgroud)
public class TextBoxWithDescription : TextBox
{
    public TextBoxWithDescription()
    {
        LabelText = String.Empty;
    }

    public string LabelText { get; set; }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var textBlock = (TextBlock)this.Template.FindName("LabelText", this);
        if (textBlock != null) textBlock.Text = this.LabelText;
    }
}
Run Code Online (Sandbox Code Playgroud)
<TaskDash:TextBoxWithDescription Grid.Row="0" Grid.Column="0"
        x:Name="textBoxDescription" 
        Text="{Binding Description, BindsDirectlyToSource=True}" LabelText="Description">
    </TaskDash:TextBoxWithDescription>
Run Code Online (Sandbox Code Playgroud)
public partial class EditTaskItem : Window
{
    private TaskItem _taskItem;

    public EditTaskItem(TaskItem taskItem) …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf xaml

2
推荐指数
1
解决办法
4199
查看次数

标签 统计

c# ×1

data-binding ×1

wpf ×1

xaml ×1