代码之家  ›  专栏  ›  技术社区  ›  fre0n

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

  •  1
  • fre0n  · 技术社区  · 16 年前

    我有两个模型:

    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>
    

    当ButtonView的DataContext也被绑定时,我在获取其可见性绑定时遇到问题。如果我不使用DataContext,那么可见性就可以正常工作(当SomeFlag切换值时,按钮的可见性也随之改变)-但是显示文本和命令不起作用。如果我绑定DataContext,那么显示文本和命令就可以工作,但是可见性就不行了。我确信这是因为当我将DataContext绑定到SomeCommandViewModel时,它希望其中存在“SomeFlag”。当然,事实并非如此。

    2 回复  |  直到 14 年前
        1
  •  4
  •   Tim Cooper    14 年前

    <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>
    
        2
  •  6
  •   Martin    16 年前

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

    您似乎要同时指定2个DataContext(UserControl.DataContext不是读作ViewButton.DataContext它找到的第一个源计数)。

    您可以显式地将给定元素的datacontext作为Kent状态 或者

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