【发布时间】:2026-01-20 23:55:01
【问题描述】:
为什么绑定到 WPF 用户控件库的 Caliburn.Micro UserControl 不能与 ContentControl 一起使用?
初始
创建新的 Wpf 应用 v4.6.2
- 安装 Nuget Caliburn.Micro v3.1.0
- 安装 Nuget Caliburn.Micro.Start v3.1.0
- 按照http://caliburnmicro.com/documentation/nuget 的解释进行改编
- 调整 app.xaml
- 删除 StartupUri
- 添加资源字典
- 检查 AppBootstrapper
- 删除 MainWindow.xaml
- 将按钮
x:Name="DoIt"添加到 ShellView.xaml - 将
public void DoIt()与 MessageBox.Show() 添加到 ShellViewModel.cs - 测试这个初始版本
✓ 检查!这运行和绑定工作......
用户控制视图
- 添加
UserControl并将其命名为例如测试UcView - 添加
Textbox并命名,例如UcValue
<UserControl x:Class="WpfApp1.Test2UcView"
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:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="200" Visibility="{Binding UcVisibility}">
<Grid >
<TextBox x:Name="UcValue" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="100"/>
<Button x:Name="UcAction" Content="Do specific" HorizontalAlignment="Left" Margin="120,10,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</UserControl>
用户控件视图模型
- 添加对应名称TestUcViewModel的类
- 将类更改为 public 并从屏幕派生并使用 Caliburn.Micro 添加
- 将具有相应名称的属性添加到
TextBox,例如UcValue
public class Test1UcViewModel : Screen
{
private string _UcValue;
public string UcValue { get => _UcValue; set { _UcValue = value; NotifyOfPropertyChange(() => UcValue); } }
public void TestBut2() {
MessageBox.Show("TestBut2");
}
}
用户控件集成
- 在 ShellViewModel 中为 ViewModel 创建公共属性并在构造函数中创建实例
-
在要绑定的视图中创建控件
- 可能性:将
ContentControl放在与ViewModel 属性同名的ShellView.xaml中(ViewModel 优先) - 可能性:编译
UserControl并将其放入ShellView.xaml并为此命名空间添加cal:Bind.Model="{Binding <ViewModel-Property-Name>}"
- 可能性:将
✓ 检查!这运行和 UserControl 的绑定工作...
但是,
...现在集成了属于 WPF 用户控件库 (dll) 的第三个 UserControl,当使用 ContentControl 的语法时,Caliburn 绑定不适用于来自 dll 的 UserControl。
public class ShellViewModel : Caliburn.Micro.PropertyChangedBase, IShell
{
private Test1UcViewModel _Test1UserControlModel;
private Test2UcViewModel _Test2UserControlModel;
private Test3UcViewModel _Test3UserControlModel;
public Test1UcViewModel Test1 { get => _Test1UserControlModel; set { _Test1UserControlModel = value; NotifyOfPropertyChange(() => Test1); } }
public Test2UcViewModel Test2 { get => _Test2UserControlModel; set { _Test2UserControlModel = value; NotifyOfPropertyChange(() => Test2); } }
public Test3UcViewModel Test3 { get => _Test3UserControlModel; set { _Test3UserControlModel = value;NotifyOfPropertyChange(() => Test3); } }
public ShellViewModel()
{
_Test1UserControlModel = new Test1UcViewModel();
Test1.UcValue = "Bubble";
_Test2UserControlModel = new Test2UcViewModel();
Test2.UcValue = "Simmer";
_Test3UserControlModel = new Test3UcViewModel();
Test3.Uc3Value = "Knocking on heavens door";
Test1.UcVisibility = Visibility.Visible;
Test2.UcVisibility = Visibility.Hidden;
Test3.UcVisibility = Visibility.Hidden;
}
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:local="clr-namespace:WpfApp1"
xmlns:TestUcLib="clr-namespace:TestUcLib;assembly=TestUcLib"
x:Class="WpfApp1.ShellView" Width="500" Height="300">
<Grid Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Input"
TextWrapping="Wrap"
VerticalAlignment="Bottom"
HorizontalAlignment="Center"
FontSize="20" />
<StackPanel>
<Button x:Name="DoIt1" Content="Do it 1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="5"/>
<Button x:Name="DoIt2" Content="Do it 2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="5"/>
<Button x:Name="DoIt3" Content="Do it 3" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="5"/>
</StackPanel>
<ContentControl x:Name="Test1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<ContentControl x:Name="Test2" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<ContentControl x:Name="Test3" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<!--This one works-->
<!--<TestUcLib:Test3UcView cal:Bind.Model="{Binding Test3}" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>-->
</Grid>
</Window>
当 UserControl 在 dll 中时,是否必须使用 <TestUcLib:Test3UcView cal:Bind.Model="{Binding Test3}" 而不是 ContentControl?
【问题讨论】:
标签: c# wpf mvvm user-controls caliburn.micro