【问题标题】:WPF Custom Control ErrorWPF 自定义控件错误
【发布时间】:2013-01-15 20:12:54
【问题描述】:

我在使用 WPF 自定义控件时遇到了一些问题,我正在尝试让它工作,但就是不明白:

这是我的问题,我正在创建一个与 TextBox 几乎相同的简单自定义控件。该控件有一个名为“Texto”的依赖属性,并且自定义控件的 XAML 和后台代码之间的绑定工作正常,代码如下:

<UserControl x:Class="WpfCustomControlLibrary1.UserControl1"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="47" d:DesignWidth="147">
<Grid Height="43" Width="142">
    <TextBox Height="23" HorizontalAlignment="Left" Margin="12,8,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Texto}"/>        
</Grid>

以及依赖属性代码:

public static readonly DependencyProperty TextoProperty = DependencyProperty.Register("Texto", typeof(string), typeof(UserControl1));

    public string Texto
    {
        get
        {
            return (string)GetValue(TextoProperty);
        }

        set
        {
            SetValue(TextoProperty, value);
        }
    }

好的,现在的问题是:当我在其他窗口中使用此控件时,我尝试将“Texto”属性绑定到视图模型(与其他所有内容一样简单),但视图模型上的属性不会改变:

ViewModel 代码:

 public class ViewModelTest
{
    public string SomeText { get; set; }
}

以及应用窗口的代码:

public ViewModelTest test;

    public Window1()
    {
        InitializeComponent();
    }        

    private void button1_Click_1(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(test.SomeText);
        MessageBox.Show(uc.Texto);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        test = new ViewModelTest();
        this.DataContext = test;
    }

以及与视图模型的属性的绑定:

<my:UserControl1 HorizontalAlignment="Left" Margin="27,12,0,0" Name="uc" VerticalAlignment="Top" Texto="{Binding Path=SomeText,Mode=TwoWay}"/>

为了更清楚,如果我在自定义控件中写“Hello”,然后按下“button1”,第一条消息什么也不显示,第二条消息显示“Hello”。

如您所见,我对此很陌生,所以我希望你们中的一些人可以帮助我。谢谢。

【问题讨论】:

    标签: c# .net wpf xaml


    【解决方案1】:

    您的绑定Texto="{Binding SomeText}" 工作正常,问题是从您的用户控件重新绑定到内部文本框。请记住,如果未修改,绑定将始终参考DataContext。但是您的 DataContext 不包含属性 Texto。你的控件有这个,要引用它,你需要一个叫做TemplateBinding的东西,但这只有在你在ControlTemplate中时才有效。你不是这样的解决方案是什么? 您可以通过将源从 DataContext 更改为具有给定名称的控件来使用特殊形式的绑定:首先给您的 UserControl 一个名称

    <UserControl x:Class="WpfCustomControlLibrary1.UserControl1"
             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" 
             mc:Ignorable="d" 
             x:Name="mainControl"
             d:DesignHeight="47" d:DesignWidth="147">
    

    现在更改绑定以引用控件,而不是控件的 DataContext。

     <TextBox Text="{Binding ElementName=mainControl, Path=Texto}"/>
    

    现在您的 ViewModel 绑定到您的用户控件,并且用户控件的内容绑定到用户控件的 Texto 属性。

    还有一个小东西,你说的自定义控件,其实是用户控件,自定义控件是别的东西。

    【讨论】:

    • 哥们,我欠你很多!谢谢,顺便说一句,如果你能推荐我一些好的 XAML 或 WPF 教程,我将不胜感激,再次感谢伙计!。
    • @RodrigoAguirreVanda 我总是可以推荐的是“WPF Binding CheatSheet”,您可以在 google 上轻松找到它。 The Unleashed WPF Book 对我和 msdn 来说当然有很大的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    相关资源
    最近更新 更多