简体   繁体   中英

Caliburn.Micro binding issue

I have a ShellView containing a LeftView, which itself contains a LeftTopView. The problem i'm having is that the LeftTopView is not showing the information from the LeftTopViewModel.

When i change the boostrapper to use DisplayRootViewFor<LeftViewModel>(); instead of the DisplayRootViewFor<ShellViewModel>(); , i will see the combobox filled with values.

Certainly i'm missing something here on why the combobox doesn't show data when the ShellView is displaying. Does Caliburn.Micro only bind single level in hierarcy?

The code is as follows:

public class ShellViewModel : Screen
{
    public LeftViewModel Left { get; set; }

    public ShellViewModel()
    {
        this.DisplayName = "The title of the application";
        Left = new LeftViewModel() { LeftTop = new LeftTopViewModel() };
    }
}

public class LeftViewModel : PropertyChangedBase
{
    public string LeftTitle { get; set; }
    public LeftTopViewModel LeftTop { get; set; }

    public LeftViewModel()
    {
        LeftTitle = "this is visible";
        LeftTop = new LeftTopViewModel();
    }
}

public class LeftTopViewModel : PropertyChangedBase
{
    public string Title { get; set; }

    public List<string> Names { get; set; }

    public string SelectedName { get; set; }

    public LeftTopViewModel()
    {
        Title = "Left-top title";
        Names = new List<string>() { "Dan", "Mark" };
        SelectedName = "Dan";
    }
}

The xaml files for above view models are defined like this.

ShellView.xaml (Window)

<Grid>
    <views:LeftView cal:Bind.Model="{Binding Left}" />
</Grid>

LeftView.xaml (UserControl)

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Label x:Name="LeftTitle" Grid.Row="0" />
    <views:LeftTopView cal:Bind.Model="{Binding LeftTop}" Grid.Row="1"/>
</Grid>

LeftTopView.xaml (UserControl)

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Label x:Name="Title" Grid.Row="0" />
    <ComboBox x:Name="Names" Grid.Row="1" />
</Grid>

Ok. it seems that i should not use cal:Bind.Model from both the ShellView and the LeftView. In the LeftView i changed the cal:Bind.Model to cal:View:Model and that made this work.

According to the tooltips on those dependency properties.

Bind.Model

Allows binding on an existing view. Use on root UserControls, Pages and Window; not in a DataTemplate.

View.Model

Dependency property for attaching a model to the UI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM