为RelativeSource绑定定义Blend Expression ViewModel定义

时间:2015-05-26 09:55:41

标签: wpf xaml mvvm user-controls blend

我有一个UserControl(MyCompositeView),我在其中添加了一些其他UserControls(MyDetailView)。为了获得智能感知和重构能力,我在d:DataContext xaml中添加了MyCompositeViewModel

要在添加新DataContext时操作UserControl,我将Visiblity绑定到RelativeSource(我的CompositeViewModel)。不幸的是,DataContext的匹配在编辑器中不适用于此绑定。

有没有办法让视觉工作室知道我的DataContext.DetailsIsVisible MyCompositeViewModel来自<UserControl xmlns:d="http://schemas.microsoft.com/expression/blend/2008" x:Class="Example.MyCompositeView" d:DataContext="{d:DesignInstance Type=MyCompositeViewModel}" d:DesignHeight="300" d:DesignWidth="300" mc:Ignorable="d"> <Grid> <detail:MyDetailView DataContext="{Binding Path=DetailViewModel}" Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.DetailsIsVisible, Converter={StaticResource BooleanToVisibilty}}" /> </Grid> </UserControl> 类型?

public class MyCompositeViewModel : IMyCompositeViewModel {

    public bool DetailisVisible{get;set;}

    public MyDetailViewModel DetailViewModel { get; }
}   

-

public class BeerSong {
    public static void main(String[] args) {
        int beerNum = 99;
        String word = "bottles";
        while (beerNum > 0)
        {
            if (beerNum == 1)
            {
                word = "bottle";
            }
            System.out.println(beerNum + " " + word + " of beer on the wall");
            System.out.println(beerNum + " " + word + " of beer");
            System.out.println("Take one down.");
            System.out.println("Pass it around.");
            beerNum = beerNum - 1;
            if (beerNum > 0)
            {
                System.out.println(beerNum +  " " + word + " of beer on the wall");
            }
            else
            {
                System.out.println("No more bottles of beer on the wall");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

尝试使用元素名称进行绑定:

<UserControl 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    x:Class="Example.MyCompositeView"
    x:Name="root"
    d:DataContext="{d:DesignInstance Type=MyCompositeViewModel}"
    d:DesignHeight="300"
    d:DesignWidth="300"
    mc:Ignorable="d">

     <Grid>
        <detail:MyDetailView 
            DataContext="{Binding Path=DetailViewModel}"
            Visibility="{Binding ElementName="root"},
                Path=DataContext.DetailsIsVisible,
                Converter={StaticResource BooleanToVisibilty}}" />
     </Grid>

 </UserControl>

或翻转属性顺序:

<UserControl 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    x:Class="Example.MyCompositeView"
    d:DataContext="{d:DesignInstance Type=MyCompositeViewModel}"
    d:DesignHeight="300"
    d:DesignWidth="300"
    mc:Ignorable="d">

     <Grid>
        <detail:MyDetailView            
            Visibility="{Binding DetailsIsVisible, Converter={StaticResource BooleanToVisibilty}}" 
            DataContext="{Binding DetailViewModel}"/>
     </Grid>

 </UserControl>