嵌套用户控件上的绑定在Caliburn Micro中不起作用

时间:2012-02-17 10:14:57

标签: wpf user-controls caliburn.micro

我有一个在shell中加载的usercontrol(X),我正在使用Screen / Conductors。控件X有一个tabcontrol,我试图加载另一个usercontrol(Y)作为TabItem。这两个控件都有ViewModel,它们还没有共享任何内容。

Usercontrol X:

[Export(typeof(IShell))]
public class CategoryViewModel : Screen, IShell
{}

控件X中的Xaml:

<TabItem Header="Award Images">
    <StackPanel>
        <Views:ImageView cal:Bind.Model="{Binding}" />
    </StackPanel>
</TabItem>

Usercontrol Y:

[Export(typeof(IShell))]
public class ImageViewModel : PropertyChangedBase, IShell
{}

当我运行应用程序时,我得到了在shell中加载的CategoryView并且工作正常。 ImageView已加载,但没有任何功能可用。

我希望通过ImageViewModel的构造函数看到我在ImageView上的ListView中放入一些虚拟数据。

非常感谢任何帮助。

感谢。

更新: CategoryView.xaml

<UserControl x:Name="CategoryViewUserControl" 
         x:Class="Central.BusinessObjects.Wpf.Views.CategoryView" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended" 
         xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" 
         xmlns:Views="clr-namespace:Central.BusinessObjects.Wpf.Views" 
         xmlns:Converters="clr-namespace:Central.BusinessObjects.Wpf.Converters" MinHeight="500" MinWidth="500">
<UserControl.Resources>
    <ResourceDictionary>            
        <Converters:CategoryTypeConverter x:Key="CategoryTypeConverter" />
    </ResourceDictionary>
</UserControl.Resources>
<StackPanel>
    <ToolBar>
        <Button Height="23" Width="75" cal:Message.Attach="SaveCategories($dataContext)">
            <StackPanel Orientation="Horizontal">
                <Image />
                <TextBlock Text="Save" />
            </StackPanel>
        </Button>           
    </ToolBar>
    <StackPanel Orientation="Horizontal">
        <ComboBox x:Name="Types" 
                  IsEditable="True" 
                  Background="Beige" 
                  IsReadOnly="True" 
                  Text="Select Award Type" 
                  DisplayMemberPath="Description"                     
                  Height="23" Width="219" />

        <ComboBox x:Name="Awards" 
                  DisplayMemberPath="Year" 
                  Background="Beige"                      
                  Height="23" Width="120" />
        <Label Content="Image" />
        <TextBlock x:Name="AwardImage" TextAlignment="Justify" MinWidth="300" Background="Beige" />
        <Button x:Name="BrowseAwardImage" Content="Browse" />
    </StackPanel>
    <TabControl>
        <TabItem Header="Award Categories">
            <StackPanel>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Label Content="Name" Grid.Column="0" Grid.Row="0" />
                    <TextBox x:Name="Description" Background="Beige" Grid.Column="1" Grid.Row="0" />

                    <Label Content="Reveal Time" Grid.Column="2" Grid.Row="0" />

                    <extToolkit:TimePicker Background="Beige" 
                                           Value="{Binding Path=RevealTime}" 
                                           Format="ShortTime" 
                                           ShowButtonSpinner="False" 
                                           Grid.Column="3" Grid.Row="0" 
                                           AllowSpin="False" />

                    <ComboBox x:Name="CategoryTypes" 
                              Background="Beige" 
                              Height="23" Width="120"
                              Grid.Column="4" Grid.Row="0" />

                    <Button Content="Add" Grid.Column="6" Grid.Row="0" 
                            cal:Message.Attach="[Event Click] = [Action Add($dataContext)]" />
                </Grid>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <ScrollViewer Grid.Row="1" Margin="0,10,0,10">
                        <DataGrid  x:Name="Categories" CanUserAddRows="False" AutoGenerateColumns="False">
                            <DataGrid.Columns>
                                <DataGridTemplateColumn Header="Category Name" MinWidth="500">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding  Path=Description}" />
                                                <TextBlock Text="(" Margin="10,0,0,0" />
                                                <TextBlock Text="{Binding Path=Descriptor, Converter={StaticResource CategoryTypeConverter}, Mode=TwoWay}" />
                                                <TextBlock Text=")" Margin="0,0,10,0" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTextColumn Header="Reveal Time" Binding="{Binding Path=RevealTime}" />
                                <DataGridTemplateColumn Header="Nominees">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Content="Edit" 
                                                    cal:Message.Attach="[Event Click] = [Action AddNominees(CategoryViewUserControl)]" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                            </DataGrid.Columns>
                        </DataGrid>
                    </ScrollViewer>
                </Grid>
            </StackPanel>
        </TabItem>
        <TabItem Header="Award Images">
            <StackPanel>
                <Views:ImageView cal:Bind.Model="{Binding Path=Central.BusinessObjects.Wpf.ViewModels.ImageViewModel}" />
            </StackPanel>
        </TabItem>
    </TabControl>
</StackPanel>

ImageView.xaml

<UserControl x:Class="Central.BusinessObjects.Wpf.Views.ImageView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Label x:Name="LabelName" Width="Auto" Grid.Column="0" Grid.Row="0"/>
    <Button x:Name="AddImages" Content="Add" Width="40" Grid.Column="1" Grid.Row="0"/>
    <ListView x:Name="SelectedImages" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="Auto">
                    <TextBlock Text="{Binding Path=ImageUri}" />
                </GridViewColumn>
                <GridViewColumn>
                    <TextBlock Text="{Binding Path=Caption}" />
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>    

目前两种视图模型完全独立。

1 个答案:

答案 0 :(得分:2)

如果没有看到更多代码,我无法肯定地说,但我认为问题是您的TabControl的{​​{1}}绑定到ItemsSource类型的集合,所以在绑定表达式中,Caliburn.Micro将尝试将Category类型的每个属性绑定到Category上的控件。

您是否尝试为每个标签设置一个类别?每个标签显示的内容略有不同?无论哪种方式,您可能应该将Tab控件绑定到视图模型的集合,而不是ImageView类型的集合。也许,Category可能会显示基于DataTemplateSelector类型的相应视图。

<强>更新

最好将Category作为ImageViewModel的属性。实例化CategoryViewModel中的ImageViewModel(将其分配给属性),然后从以下位置更改绑定表达式:

CategoryViewModel

为:

<Views:ImageView cal:Bind.Model="{Binding Path=Central.BusinessObjects.Wpf.ViewModels.ImageViewModel}" />

以下内容也可能有效:

<Views:ImageView cal:Bind.Model="{Binding MyImageViewModelProperty}" />