WPF和Caliburn不绑定嵌套用户控件
我只是试图创建一个用户控件使用WPF/Caliburn UserControl内的绑定,但我有麻烦,正确绑定嵌套UserControl。WPF和Caliburn不绑定嵌套用户控件
基本布局是一个ShellViewModel:Conductor,并且在ShellView中有一个ContentControl,它由加载UserControl(PageViewModel)的ShellViewModel.ActivateItem方法填充,并且在PageView UserControl中有一个嵌套的UserControl,名为“ SimpleControl”。
当页面加载时它会起作用(它在嵌套UserControl中显示“Initial Text Value”字符串),但它不会绑定到PageView上的PropertyChanged事件(并且在测试时从不更新其值按钮被按下)。父用户控件(PageView)中的标签按预期正确绑定/更新。
PageView.xaml:
<UserControl x:Class="CaliburnTest.Views.PageView"
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"
xmlns:local="clr-namespace:CaliburnTest.Views"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Background="Aqua">
<Label Content="{Binding TextLabelTest, FallbackValue=DEFAULT}"></Label>
<local:SimpleControl cal:Bind.Model="WPFCaliburnTemplate.Views.PageView" TextValue="{Binding TextLabelTest}"></local:SimpleControl>
<Button Name="UpdateTextButton">Update Text</Button>
</StackPanel>
</UserControl>
PageViewModel.cs:
using Caliburn.Micro;
namespace CaliburnTest.ViewModels
{
public class PageViewModel : Screen
{
private string _textLabelTest;
public string TextLabelTest
{
get { return _textLabelTest; }
set
{
_textLabelTest = value;
NotifyOfPropertyChange(() => TextLabelTest);
}
}
public PageViewModel()
{
TextLabelTest = "Initial Text Value";
}
public void UpdateTextButton()
{
TextLabelTest = "Updated Text Value";
}
}
}
SimpleControl.xaml:
<UserControl x:Class="CaliburnTest.Views.SimpleControl"
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"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform.Core"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Border Margin="10" BorderThickness="1" BorderBrush="#FF9A9A9A">
<StackPanel>
<Label Name="TextLabel"></Label>
</StackPanel>
</Border>
</UserControl>
最后SimpleControl.xaml.cs:
using System.Windows;
using System.Windows.Controls;
namespace CaliburnTest.Views
{
public partial class SimpleControl : UserControl
{
public SimpleControl()
{
InitializeComponent();
}
public static DependencyProperty TextValueProperty = DependencyProperty.Register("TextValue", typeof(string), typeof(SimpleControl),
new FrameworkPropertyMetadata("", TextValueChangedCallBack));
public string TextValue
{
get { return (string)GetValue(TextValueProperty); }
set
{
SetValue(TextValueProperty, value);
Refresh();
}
}
protected static void TextValueChangedCallBack(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
(sender as SimpleControl).TextValue = (string)e.NewValue;
}
private void Refresh()
{
TextLabel.Content = TextValue;
}
}
}
我一整天都在喋喋不休,无法弄清楚我做错了什么。我尝试了不同的DataContexts,RelativeSources,以及我可以在SO和Google上找到的其他任何组合,但我仍然缺乏。我有一个更复杂的自定义UserControl我正在尝试使用,但我创建了这个简单的示例来尝试找出问题。
我以各种组合发挥各地,我相信我发现,似乎工作,虽然我不完全知道为什么结果:
首先,我改变了我被绑定“TextValue”的方式依赖项属性为:
<local:SimpleControl TextValue="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}, Path=DataContext.TextLabelTest}" />
但是,这仍然只适用于初始值,并且在更新字符串时绑定未更新。最后,我不得不编辑SimpleControl.xaml.cs像这样:
protected static void TextValueChangedCallBack(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// (sender as SimpleControl).TextValue = (string)e.NewValue;
(sender as SimpleControl).Refresh();
}
正如你所看到的,我注释掉线设置SimpleControl.TextValue(反过来设置TextValueProperty的DependencyProperty)和简称SimpleControl.Refresh()方法来更新标签。我不确定为什么设置TextValueProperty会破坏DependencyProperty(尽管在字符串更新时已经设置了它),但我认为我需要去阅读一些MSDN文章!