简体   繁体   English

MVVM 绑定在设计时不起作用

[英]MVVM Binding not working in design-time

I tried to bind the visibility of a few buttons to some booleans in a viewmodel and make it work in design-time.我试图将几个按钮的可见性绑定到视图模型中的一些布尔值,并使其在设计时工作。 I did this several times and never had a problem, but now it does not work and I don't have a clue why.我做了几次,从来没有遇到过问题,但现在它不起作用,我不知道为什么。 Note that everything works fine when I run the application.请注意,当我运行应用程序时一切正常。

I extracted the essence into a separate app and it still doesn't work!我将精华提取到一个单独的应用程序中,它仍然不起作用!

My XAML:我的 XAML:

<Window x:Class="BindingTest.MainWindow"
        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"
        xmlns:bindingTest="clr-namespace:BindingTest"
        Title="MainWindow" Height="350" Width="525"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance bindingTest:TestViewModel, d:IsDesignTimeCreatable=true}">
    <Grid>
        <Grid.Resources>
            <BooleanToVisibilityConverter x:Key="BoolToVis"/>
        </Grid.Resources>
        <StackPanel>
            <Button Visibility="{Binding IsButton1Visible, Converter={StaticResource BoolToVis}}">Hallo 1</Button>
            <Button Visibility="{Binding Button2Visibility}">Hallo 2</Button>
            <Button>Hallo 3</Button>
        </StackPanel>
    </Grid>
</Window>

My ViewModel:我的视图模型:

public class TestViewModel
{
    public bool IsButton1Visible
    {
        get { return true; }
    }

    public Visibility Button2Visibility
    {
        get { return Visibility.Hidden; }
    }
}

To make it work in the real app, i added to the constructor of the XAML:为了让它在真正的应用程序中工作,我添加到 XAML 的构造函数中:

DataContext = new TestViewModel();

This is all pretty simple, but why is it not working in the designer??这一切都很简单,但为什么在设计器中不起作用?? I always got this to work in the past and now I tried for hours... It's not the only binding not working, but why is not even this working??过去我总是让这个工作,现在我尝试了几个小时......这不是唯一不工作的绑定,但为什么连这个都不工作?

Expected Result:预期结果:

  • Button1 visible Button1 可见
  • Button2 hidden Button2 隐藏
  • Button3 visible Button3 可见

Result in the designer:设计器中的结果:

  • Button1 collapsed Button1 折叠
  • Button2 visible Button2 可见
  • Button3 visible Button3 可见

UPDATE: The problem with design-time DataContext bindings not working is due to mistakenly using the d: namespace prefix for the IsDesignTimeCreatable property.更新:设计时 DataContext 绑定不起作用的问题是由于错误地使用了IsDesignTimeCreatable属性的d:命名空间前缀。

So, change所以,改变

d:IsDesignTimeCreatable=True

to

IsDesignTimeCreatable=True

and everything should be fine.一切都应该没问题。 (My bug report filed with MS will be updated with this information as well.) (我提交给 MS 的错误报告也将使用此信息进行更新。)


As this problem with the design-time DataContext bindings not working was consistently reproducable in VS2015 as well as VS2013 when IsDesignTimeCreatable is prefixed with the d: namespace (see comments below the question), i decided to file a bug report with Microsoft: Issue #1651633 "WPF designer: Designtime DataContext bindings broken?"由于设计时 DataContext 绑定不起作用的问题在 VS2015 和 VS2013 中始终可重现,当IsDesignTimeCreatabled:命名空间为前缀时(请参阅问题下方的评论),我决定向 Microsoft 提交错误报告: 问题 # 1651633 “WPF 设计器:Designtime DataContext 绑定损坏?” . .

Lets see how Microsofts will follow-up with regard to this issue.让我们看看微软将如何跟进这个问题。 It should be addressed, since such an issue can catch a developer off-guard and make him reinstall Visual Studio for no benefit... ;)它应该得到解决,因为这样的问题会使开发人员措手不及,并使他重新安装 Visual Studio 没有任何好处......;)

If anything is OK with the code, there could be an other tricky problem:如果代码没有问题,可能还有其他棘手的问题:

In the Designer of Visual Studio a new button was introduced in 2019, which enables / disables the preview of design data .在 Visual Studio 的设计器中,2019 年引入了一个新按钮,可以启用/禁用设计数据的预览

It's a very tiny button on botton left of the designer window.这是位于设计器窗口左侧的一个非常小的按钮。 It's tooltip is "Enable Project Code".它的工具提示是“启用项目代码”。

Click this important toggle button to see the design data.单击这个重要的切换按钮以查看设计数据。 (But be aware that you don't disable it, if it's already enabled.) (但请注意,如果它已启用,则不要禁用它。)

用于启用设计器数据的按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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