这是一个简化的示例。我有一个用户控件,包含一个“浏览到文件夹”功能,使用文本框和按钮。点击按钮将打开浏览对话框,并且基本上会填入文本框。Caliburn Micro Message.attach不考虑datacontext的更改
<UserControl x:Class="MyUserControl"
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="http://www.caliburnproject.org"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<!-- Folder -->
<TextBlock>Path</TextBlock>
<DockPanel LastChildFill="True" Grid.Column="1">
<Button DockPanel.Dock="Right" cal:Message.Attach="[Event Click] = [Action BrowseHotFolder()]" Content="..." HorizontalAlignment="Left" Width="25" Height="25" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,5,0"/>
<TextBox Text="{Binding HotFolderPath, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />
</DockPanel>
</Grid>
我有一个包含多个对象的列表框。选定的对象将作为datacontext输入到此用户控件中。
<Window>
...
<Listbox ItemsSource="{Binding Items, Mode=OneWay}" SelectedItem="{Binding SelectedItem}">
...
<view:MyUserControl DataContext="{Binding SelectedItem}" />
</Window>
现在,让我们说我有两个项目在我的列表框中,我有第一个选择。我在MyUserControl的文本框中填写“foo”。然后我选择第二项,并填写“栏”。数据绑定工作正常,并且两项都设置了正确的值。如果我然后单击第一个浏览按钮并选择一个文件夹,它会将第一个项目的文本框更改为所选路径。但是,如果我选择第二个项目并浏览到一个文件夹,它也将更改第一个项目的文本框。
我的猜测是消息附加语法不会调用正确的Item上的浏览操作。它无视datacontext(当前选定的项目),只是使用第一个。
我该怎么办?