简体   繁体   English

WPF XAML - 文本框的设计时间和可见性

[英]WPF XAML - Design time and visibility of textbox

I use Visual Studio 2019 with WPF / MVVM.我将 Visual Studio 2019 与 WPF/MVVM 结合使用。

I have set a trigger to a textbox to control its visibiliy.我已经为一个文本框设置了一个触发器来控制它的可见性。 And during runtime this works well, the trigger checks the state of a radiobutton and sets the visibiliy of the textbox accoring to the radiobutton's state.并且在运行时这运行良好,触发器检查单选按钮的状态并根据单选按钮的状态设置文本框的可见性。

But during designtime this textbox is not visible.但在设计期间,此文本框不可见。 How could I make this textbox to be visible during designtime ?我怎样才能让这个文本框在设计时可见?

This is the XAML I have for the trigger:这是我用于触发器的 XAML:

<Style x:Key="text" TargetType="TextBox">

    <Style.Triggers>

       <DataTrigger Binding="{Binding ElementName=Radiobutton1, Path=IsChecked}" Value="true">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>

        <DataTrigger Binding="{Binding ElementName=Radiobutton1, Path=IsChecked}" Value="false">
            <Setter Property="Visibility" Value="Collapsed"/>
        </DataTrigger>

    </Style.Triggers>
</Style>


<TextBox Style="{StaticResource text}"  Text="test..... />

I found this article https://social.msdn.microsoft.com/Forums/en-US/cacc5c30-8aa0-43c5-ad07-b063028653a2/designmode-and-visibility?forum=wpf and did some tests using "DesignerProperties.IsInDesignMode" but I can not make this run,I get errors like "datatrigger can not be added to setterbasecollection".我发现这篇文章https://social.msdn.microsoft.com/Forums/en-US/cacc5c30-8aa0-43c5-ad07-b063028653a2/designmode-and-visibility?forum=wpf并使用“DesignerProperties.IsInDesignMode "但我无法运行此程序,出现诸如“无法将数据触发器添加到 setterbasecollection”之类的错误。

Also I don't know if "DesignerProperties.IsInDesignMode" is the right approach...另外我不知道“DesignerProperties.IsInDesignMode”是否是正确的方法......

This is a workaround: 这是一种解决方法:

<Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=Radiobutton1, Path=IsChecked}" Value="true">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding ElementName=Radiobutton1, Path=IsChecked}" Value="false">
            <Setter Property="Visibility" Value="Collapsed"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Designtime}" Value="true">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>

then in Viewmodel: 然后在Viewmodel中:

public bool Designtime { get; set; }

public ViewModel()
{
    if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
    {
        Designtime = true;
    }
}

And in the Window Tag 并在窗口标签中

d:DataContext="{d:DesignInstance {x:Type local:ViewModel},IsDesignTimeCreatable=True}"

I think the answer is even simpler.我认为答案更简单。 By adding d:Visibility="Visible" , the textbox will be visible at design time.通过添加d:Visibility="Visible" ,文本框将在设计时可见。

<TextBox d:Visibility="Visible" Style="{StaticResource text}"  Text="test..... />

You can use the Blend namespace IsHidden attribute:您可以使用 Blend 命名空间IsHidden属性:

  • add the Blend namespace if missing: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"如果缺少,请添加 Blend 命名空间: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  • add the d:IsHidden="True" attribute on the element you want to hide at design time在设计时要隐藏的元素上添加d:IsHidden="True"属性

Example:例子:

<TextBox Style="{StaticResource text}"  Text="test....." d:IsHidden="True"/>

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

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