使用的DependencyProperty能见度绑定 [英] Visibility Binding using DependencyProperty

查看:150
本文介绍了使用的DependencyProperty能见度绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一些简单的代码使用ToggleButton.IsChecked属性来设置TextBlock的可见性。它工作正常。由于这并不完全适合我的程序结构,我想另一个TextBlock的知名度绑定的本一个的DependencyProperty。它编译罚款,但它产生任何影响。我做错了什么,只是不知道是什么。



XAML

 <窗口x:类=ToggleButtonTest.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =HTTP: //schemas.microsoft.com/winfx/2006/xaml
标题=主窗口WIDTH =200HEIGHT =100>
< Window.Resources>
< BooleanToVisibilityConverter X:键=BooleanToVisibilityConverter/>
< /Window.Resources>
<&StackPanel的GT;
<切换按钮X:NAME =切换按钮CONTENT =切换
=器isChecked真选中=toggleButton_Checked/>
< TextBlock的文本=一些文本
能见度={结合器isChecked,
的ElementName =切换按钮,
转换= {StaticResource的BooleanToVisibilityConverter}}/>
< TextBlock的文本=更多文本
能见度={结合ShowMoreText,
的ElementName =此,
转换= {StaticResource的BooleanToVisibilityConverter}}/>
< / StackPanel的>
< /窗GT;



C#



 使用System.Windows; 

命名空间ToggleButtonTest
{
公共部分类主窗口:窗口
{
静态主窗口()
{
FrameworkPropertyMetadata元=
新FrameworkPropertyMetadata(真,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);

ShowMoreTextProperty =
DependencyProperty.Register(ShowMoreText,
typeof运算(布尔)的typeof(主窗口),元);
}

公共主窗口()
{
的InitializeComponent();
}

公共静态只读的DependencyProperty ShowMoreTextProperty;
公共BOOL ShowMoreText
{
得到
{
返回(布尔)的GetValue(ShowMoreTextProperty);
}

{
的SetValue(ShowMoreTextProperty,值);
}
}

私人无效toggleButton_Checked(对象发件人,RoutedEventArgs E)
{
ShowMoreText = toggleButton.IsChecked.Value;
}
}
}



编辑:



有过这个回答,我想我的张贴工作代码...



XAML

 <窗口x:类=ToggleButtonTest.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口WIDTH =200HEIGHT =100
产品名称=thisWindow>
< Window.Resources>
< BooleanToVisibilityConverter X:键=BooleanToVisibilityConverter/>
< /Window.Resources>
<&StackPanel的GT;
<切换按钮X:NAME =切换按钮
含量=切换
=器isChecked{绑定路径= ShowMoreText,的ElementName = thisWindow}/>
< TextBlock的文本=更多文本
能见度={绑定路径= ShowMoreText,
的ElementName = thisWindow,
转换器= {StaticResource的BooleanToVisibilityConverter}}/>
< / StackPanel的>
< /窗GT;



C#



 使用System.Windows; 

命名空间ToggleButtonTest
{
公共部分类主窗口:窗口
{
公共主窗口()
{
的InitializeComponent() ;
}

公共静态只读的DependencyProperty ShowMoreTextProperty =
DependencyProperty.Register(ShowMoreText的typeof(布尔),
typeof运算(主窗口),新FrameworkPropertyMetadata(真实的,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

公共BOOL ShowMoreText
{
得到
{
返回(布尔)的GetValue(ShowMoreTextProperty);
}

{
的SetValue(ShowMoreTextProperty,值);
}
}
}
}


解决方案

的ElementName 必须真正是一种元素名称。 这个不飞。幸运的是,你有类型的元素主窗口这里有一个 ShowMoreText 属性:根窗口元素。



输入窗口的名称,并用其作为的ElementName ,如下:

 <窗口x:类=ToggleButtonTest.MainWindow 
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口WIDTH =200HEIGHT =100
X:NAME =thisWindow>
< Window.Resources>
< BooleanToVisibilityConverter X:键=BooleanToVisibilityConverter/>
< /Window.Resources>
<&StackPanel的GT;
<切换按钮X:NAME =切换按钮CONTENT =切换
=器isChecked真选中=toggleButton_Checked/>
< TextBlock的文本=一些文本
能见度={结合器isChecked,
的ElementName =切换按钮,
转换= {StaticResource的BooleanToVisibilityConverter}}/>
< TextBlock的文本=更多文本
能见度={结合ShowMoreText,
的ElementName = thisWindow,
转换= {StaticResource的BooleanToVisibilityConverter}}/>
< / StackPanel的>
< /窗GT;

请注意,您可以用做同样的的RelativeSource自,但我更喜欢上面的方法。


I've some simple code below that uses a ToggleButton.IsChecked property to set the Visibility of a TextBlock. It works fine. Since this doesn't quite fit in with my program's structure, I'm trying to bind the visibility of another TextBlock to a DependencyProperty of "this". It compiles fine, but it produces no effect. I'm doing something wrong, just not sure what.

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>

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
        {
            get
            {
                return (bool)GetValue(ShowMoreTextProperty);
            }
            set
            {
                SetValue(ShowMoreTextProperty, value);
            }
        }

        private void toggleButton_Checked(object sender, RoutedEventArgs e)
        {
            ShowMoreText = toggleButton.IsChecked.Value;
        }
    }
}

Edit:

Having had this answered, I want to post my working code...

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"
    Name="thisWindow">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </Window.Resources>
    <StackPanel>
        <ToggleButton x:Name="toggleButton" 
                Content="Toggle"
                IsChecked="{Binding Path=ShowMoreText, ElementName=thisWindow}"/>
        <TextBlock Text="More Text"
                   Visibility="{Binding Path=ShowMoreText, 
                   ElementName=thisWindow,
                   Converter={StaticResource BooleanToVisibilityConverter}}"/>
    </StackPanel>
</Window>

C#

using System.Windows;

namespace ToggleButtonTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty ShowMoreTextProperty =
            DependencyProperty.Register("ShowMoreText", typeof(bool),
            typeof(MainWindow), new FrameworkPropertyMetadata(true,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public bool ShowMoreText
        {
            get
            {
                return (bool)GetValue(ShowMoreTextProperty);
            }
            set
            {
                SetValue(ShowMoreTextProperty, value);
            }
        }
    }
}

解决方案

ElementName must really be an element name. this doesn't fly. Fortunately, you do have an element of type MainWindow here with a ShowMoreText property: the root Window element.

Give the Window a name and use that as ElementName, as below:

<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"
     x:Name="thisWindow">
<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=thisWindow, 
                    Converter={StaticResource BooleanToVisibilityConverter}}"/>
</StackPanel>
</Window>

Note that you can do the same using RelativeSource Self, but I prefer the method above.

这篇关于使用的DependencyProperty能见度绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆