将可见性绑定到与DataContext不同的源

时间:2010-08-23 17:21:03

标签: c# .net wpf xaml binding

我有两个ViewModel:

public class CommandViewModel
{
    public string DisplayName { get; set; }
    public ICommand Command { get; set; }
}

public class SomeViewModel : INotifyPropertyChanged
{
    private bool someFlag;
    private CommandViewModel someCommand;

    public bool SomeFlag
    {
        get
        {
            return someFlag;
        }
        set
        {
            if (value == someFlag)
                return;
            someFlag = value;
            OnPropertyChanged("SomeFlag");
        }
    }

    public CommandViewModel SomeCommandViewModel
    {
        get
        {
            if (someCommand == null)
            {
                someCommand = new CommandViewModel();
                // TODO: actually set the DisplayName and Command
            }
            return someCommand;
        }
    }
}

我有两个相应的观点:

<UserControl x:Class="ButtonView"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="28" d:DesignWidth="91">
    <Button Content="{Binding Path=DisplayName}" Command="{Binding Path=Command}" />
</UserControl>

<UserControl x:Class="SomeView"
    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"
    mc:Ignorable="d" Height="125" Width="293" />

    <ViewButton
        Visibility="{Binding Path=SomeFlag, Converter={StaticResource BoolToVisibilityConverter}}"
        DataContext="{Binding Path=SomeCommandViewModel}" />
</UserControl>

当我的DataContext也被绑定时,我遇到了阻止ButtonView的Visibility绑定的问题。如果我退出DataContext,Visibility工作正常(当SomeFlag切换值时,按钮的可见性随之改变) - 但显示文本和命令不起作用。如果我绑定DataContext,显示文本和命令工作,但可见性不起作用。我敢肯定,当我将DataContext绑定到SomeCommandViewModel时,它需要“SomeFlag”存在于其中。当然,事实并非如此。

2 个答案:

答案 0 :(得分:6)

如果设置任何给定元素的DataContext,则此元素的每个绑定(包括子元素)将使用DataContext作为源,除非您明确地给出另一个源。

您似乎要做的是一次指定2个DataContext(UserControl.DataContext不会被读取,因为ViewButton.DataContext已设置且它找到的第一个源计数。)

您可以明确地将给定元素的datacontext视为Kent状态 要么 您可以明确指定源。 e.g。

<ViewButton
    Visibility="{Binding Path=SomeFlag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Converter={StaticResource BoolToVisibilityConverter}}"
    DataContext="{Binding Path=SomeCommandViewModel}" />

答案 1 :(得分:4)

我不宽恕你的设计,但这可以解决你的问题:

<UserControl x:Name="root"
    x:Class="SomeView"
    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"
    mc:Ignorable="d" Height="125" Width="293" />

    <ViewButton
        Visibility="{Binding Path=DataContext.SomeFlag, Converter={StaticResource BoolToVisibilityConverter}, ElementName=root}"
        DataContext="{Binding Path=SomeCommandViewModel}" />
</UserControl>