【问题标题】:User Control Content Presenter binding issue用户控制内容展示器绑定问题
【发布时间】:2014-05-22 19:03:33
【问题描述】:

我正在创建将充当其他控件的容器的用户控件。我在 StackOverflow 上找到了代码,并使用该示例创建了用户控件。 这是我的用户控件的代码。

 <UserControl x:Class="ClassLibrary1.UserControl1"
         xmlns:local="clr-namespace:ClassLibrary1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <UserControl.Template>
    <ControlTemplate>
        <StackPanel>
            <Button Content="Custom Control Text Area 1" IsEnabled="{Binding IsEnabled}"  />
            <ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"/>
            <TextBlock Text="Custom Control Text Area 2" />
        </StackPanel>
    </ControlTemplate>
</UserControl.Template>

代码如下

namespace ClassLibrary1
{
   public partial class UserControl1 : UserControl
   {
       public UserControl1()
       {
          InitializeComponent();
       }
   }
}

}

然后我在其他 windows 应用程序项目中添加了这个用户控件,并在这个用户控件中添加了按钮,所以代码如下所示

<StackPanel>
    <l:UserControl1>
        <StackPanel >
            <Button Click="Button_Click" Content="Click"/>
            <Button Content="My Control's Content" IsEnabled="{Binding Isbuttonenable}"/>
        </StackPanel>
    </l:UserControl1>

</StackPanel>

现在我的 Windows 应用程序项目工作正常,按钮控件能够基于 Isbuttonenable valye 启用/禁用。

现在我更新了用户控件并将 ViewModel 添加到用户控件。这是用户控件代码背后的代码

namespace ClassLibrary1
{
   public partial class UserControl1 : UserControl
   {
       UserControl1ViewModel model = new UserControl1ViewModel();
       public UserControl1()
       {
          InitializeComponent();
         this.DataContext = model;
       }
    }
} 

现在,当我在我的 Windows 应用程序项目中使用此更新后的控件时,数据绑定不再起作用。按钮现在不再能够基于 Isbuttonenable 值启用/禁用。这可能是什么解决方案。我想将 ViewModel 保留在我的用户控件中。

我尝试了很多方法和护目镜,但无法找到解决方案。

【问题讨论】:

  • 我尝试了很多方法,但没有任何效果。然后我找到解释:“TemplateBinding 不支持双向绑定,只有 Binding 支持。即使使用 BindsTwoWayBeDefault 选项,它也不支持双向绑定。”看到这个stackoverflow.com/questions/5913176/…

标签: wpf user-controls contentpresenter


【解决方案1】:

当您从其自己的代码中设置 UserControl 的 DataContext 时,这也会影响该控件的任何使用中的绑定。最初您的&lt;l:UserControl1&gt; 使用有一个继承的DataContext,它可能有一个Isbuttonenable 属性,您正在看到它的工作。当您为 DataContext 设置特定值时,相当于将您的 XAML 更改为以下内容:

<StackPanel>
    <l:UserControl1 DataContext="{StaticResource MyUserControl1ViewModelInstance}">
        <StackPanel >
            <Button Click="Button_Click" Content="Click"/>
            <Button Content="My Control's Content" IsEnabled="{Binding Isbuttonenable}"/>
        </StackPanel>
    </l:UserControl1>

</StackPanel>

这样看很明显,绑定现在将在 UserControl1ViewModel 对象上寻找 Isbuttonenable 属性。

如果您想为您的 UserControl 使用内部 VM,最好将其设置为 UserControl1 上的新自定义 DependencyProperty,并更改那些打算使用它的绑定以使用 ElementNameRelativeSource绑定来专门访问该属性而不是隐式的DataContext

我还建议您在此处重新考虑您的整体设置。 UserControl 的主要优点是您可以在 UserControl.xaml 文件中设置一组特定的内容,并在代码隐藏中对其进行操作,或者按原样重新使用它。如果您要像这样在外部设置Content,只需使用基本ContentControl 实例并设置模板来自定义它。

【讨论】:

    猜你喜欢
    • 2013-01-28
    • 2012-11-02
    • 2010-12-09
    • 2020-06-16
    • 2011-04-07
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多