相关疑难解决方法(0)

如何反转BooleanToVisibilityConverter?

BooleanToVisibilityConverter在WPF中使用a 将Visibility控件的属性绑定到a Boolean.这工作正常,但我想要一个控件隐藏如果布尔是true,并显示它是否false.

.net wpf binding visibility

128
推荐指数
8
解决办法
9万
查看次数

使用DependencyProperty进行可见性绑定

我在下面的一些简单代码中使用了ToggleButton.IsChecked属性来设置TextBlock的可见性.它工作正常.由于这不适合我的程序结构,我试图将另一个TextBlock的可见性绑定到"this"的DependencyProperty.编译很好,但没有效果.我做错了什么,只是不确定是什么.

XAML

<Window x:Class="ToggleButtonTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Width="200" Height="100">
<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<StackPanel>
    <ToggleButton x:Name="toggleButton" Content="Toggle"
                  IsChecked="True" Checked="toggleButton_Checked"/>
    <TextBlock Text="Some Text"
               Visibility="{Binding IsChecked, 
               ElementName=toggleButton,
               Converter={StaticResource BooleanToVisibilityConverter}}"/>
    <TextBlock Text="More Text"
               Visibility="{Binding ShowMoreText, 
               ElementName=this, 
               Converter={StaticResource BooleanToVisibilityConverter}}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

C#

using System.Windows;

namespace ToggleButtonTest
{
    public partial class MainWindow : Window
    {
        static MainWindow()
        {
            FrameworkPropertyMetadata meta = 
                new FrameworkPropertyMetadata(true,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);

            ShowMoreTextProperty = 
                DependencyProperty.Register("ShowMoreText", 
                typeof(bool), typeof(MainWindow), meta);
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty ShowMoreTextProperty;
        public bool ShowMoreText
        { …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf dependency-properties

1
推荐指数
1
解决办法
5740
查看次数