【问题标题】:2 identical bindings....One works and the other not [duplicate]2个相同的绑定....一个有效,另一个无效
【发布时间】:2019-11-22 08:44:57
【问题描述】:

我有一个包含 2 个用户控件的 MainWindow:Ready 和 Welcome。我想显示其中一个,具体取决于我的视图模型中的枚举值。

当应用程序启动时,应该只有 Welcome 可见,但两个部分都会显示。在调试中,我看到正在调用 ShowWelcome 属性值,但从来没有调用 ShowReady,就像它没有绑定一样。

我不明白为什么 Ready 用户控件仍然显示,即使 viewmodel 的 ShowReady 属性设置为 false。

MainWindow.xaml:

<Window x:Class="xyz.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:xyz.Views"
        mc:Ignorable="d"
        Title="app" Icon="/Assets/images/wcC.png"
        Height="800" Width="240" ResizeMode="NoResize"
        Loaded="Window_Loaded">
<StackPanel Orientation="Vertical">
    <StackPanel.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    </StackPanel.Resources>

    <local:Ready x:Name="ucReady" Visibility="{Binding ShowReady, Converter={StaticResource BoolToVisConverter}}" />
    <local:Welcome x:Name="ucWelcome" Visibility="{Binding ShowWelcome, Converter={StaticResource BoolToVisConverter}}" />
</StackPanel>
</Window>

MainWindow.xaml.cs

namespace xyz.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //ServerApiCaller api;
        ConfigModelApp configApp;
        MainWindowVM vm;

        public MainWindow(IOptionsMonitor<ConfigModelApp> configApp, ServerApiCaller api)
        {
            this.configApp = configApp.CurrentValue;

            vm = new MainWindowVM();
            DataContext = vm;

            InitializeComponent();

        }

        void Window_Loaded(object sender, RoutedEventArgs e)
        {
            vm.UpdateWindowContext(this.configApp.UserId);
        }

        internal void UpdateWindowContext(ConfigModelApp newConfig)
        {
            configApp = newConfig;
            vm.UpdateWindowContext(newConfig.UserId);
        }
    }
}

MainWindowVM.cs

namespace xyz.ViewModels
{
    public enum MainWindowContextEnum
    {
        Unknown,
        Welcome,
        Ready,
        Drafting,
        Playing,
    }

    public class MainWindowVM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public MainWindowContextEnum MainWindowContext { get; set; } = MainWindowContextEnum.Welcome;
        public bool ShowWelcome => MainWindowContext == MainWindowContextEnum.Welcome;
        public bool ShowReady => MainWindowContext == MainWindowContextEnum.Ready;

        public void UpdateWindowContext(string userId)
        {
            MainWindowContext = Guid.TryParse(userId, out Guid g) ? MainWindowContextEnum.Ready : MainWindowContextEnum.Welcome;

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ShowReady)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ShowWelcome)));
        }
    }
}

【问题讨论】:

  • 您是否将DataContext 设置在Ready 的某个位置?
  • 您应该会在 Visual Studio 的输出窗口中看到一些绑定错误消息。
  • Visibility="{Binding DataContext.ShowReady, RelativeSource={RelativeSource AncestorType=Window}, Converter={StaticResource BoolToVisConverter}}" 工作吗?
  • @mm8 是的,Ready UC 有一个视图模型,我将它设置为它的 DataContext...如果我从 Ready UC 代码隐藏中删除 vm 分配,一切正常...你能解释一下,因为我不明白:O 在用户控件中设置数据上下文会破坏其在主窗口中的可见性?
  • 明确设置 UserControl 的 DataContext 意味着它将不再从定义 ShowReady 属性的窗口继承其 DataContext。

标签: c# .net wpf


【解决方案1】:

如果Visibility="{Binding DataContext.ShowReady, RelativeSource={RelativeSource AncestorType=Window}, Converter={StaticResource BoolToVisConverter}}" 有效,则您明确设置了ReadyDataContext

您不应该这样做,因为这意味着UserControl 将不再从定义ShowReady 属性的窗口继承其DataContext。这就是绑定失败的原因。

UserControl 通常应该继承它的DataContext

【讨论】:

  • 谢谢!今天我了解到 WPF 用户控件继承了它们的数据上下文 :)
猜你喜欢
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 2020-07-26
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
相关资源
最近更新 更多