简体   繁体   English

WPF中的多个布尔到可见性转换器

[英]Multiple Bool to Visibility Converter in WPF

I'm pretty new to WPF.我对 WPF 很陌生。 I'm writing small applcation based on Demo from ToggleSwitch avaiable on Codeplex: https://toggleswitch.codeplex.com/我正在编写基于 Codeplex 上 ToggleSwitch Demo 的小应用程序: https ://toggleswitch.codeplex.com/

Basically there are 4 Radio buttons bind with 4 xaml files (as it is in the Demo from link I wrote above).基本上有 4 个单选按钮与 4 个 xaml 文件绑定(就像我在上面写的链接的演示中一样)。 Let's called them Base1.xml, Base2.xml etc.我们称它们为 Base1.xml、Base2.xml 等。

Then I wanted to make the same thing for one of these files.然后我想为这些文件之一做同样的事情。 I put in Base2.xml 3 radio buttons for 3 xamls - Sub1.xml Sub2.xml Sub3.xml.我为 3 个 xamls 放入了 Base2.xml 3 个单选按钮 - Sub1.xml Sub2.xml Sub3.xml。 I copied the logic and then I encountered problem.我复制了逻辑,然后遇到了问题。 One of Subx.xml are always visible. Subx.xml 之一始终可见。

I though that this is visible/collapsed problem and it should be visible/hidden so I tried solution from this link: http://www.rhyous.com/2011/02/22/binding-visibility-to-a-bool-value-in-wpf/我认为这是可见/折叠的问题,它应该是可见/隐藏的,所以我尝试从这个链接解决: http : //www.rhyous.com/2011/02/22/binding-visibility-to-a-bool- wpf中的价值/

But it doesn't work...但它不起作用...

Here's my main xaml:这是我的主要 xaml:

<ResourceDictionary>
    [...]
        <BooleanToVisibilityConverter x:Key="VisibilityConverter"/>
</ResourceDictionary>
<Grid>
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="LinksStackPanel" Orientation="Horizontal">
            <RadioButton Style="{StaticResource LinkStyle}" x:Name="VLink1" Content="LINK1" Margin="0,0,25,0" IsChecked="True" />
            <RadioButton Style="{StaticResource LinkStyle}" x:Name="VLink2" Content="LINK2" Margin="0,0,25,0"/>
            <RadioButton Style="{StaticResource LinkStyle}" x:Name="VLink3" Content="LINK3" Margin="0,0,25,0"/>
            <RadioButton Style="{StaticResource LinkStyle}" x:Name="VLink4" Content="LINK4" Margin="0,0,25,0"/>
        </StackPanel>
        <Grid Grid.Row="1" Margin="0,10,0,0">
            <MyApp:Link1 Visibility="{Binding ElementName=Link1, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>
            <MyApp:Link2 Visibility="{Binding ElementName=Link2, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>
            <MyApp:Link3 Visibility="{Binding ElementName=Link3, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>
            <MyApp:Link4 Visibility="{Binding ElementName=Link4, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>
        </Grid>
    </Grid>
</Grid>

And here's my Link2.xaml:这是我的 Link2.xaml:

  <UserControl.Resources>
    <ResourceDictionary>
       [...]
        <BooleanToVisibilityConverter x:Key="VisibilityConverter"/>
    </ResourceDictionary>
   </UserControl.Resources>

    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="LinksStackPanel" Orientation="Horizontal" Margin="0,0,0,0" >
            <RadioButton Style="{StaticResource LinkStyle}" x:Name="vSubLink1"     Content="SubLink1"     Margin="25,0,25,0"/>
            <RadioButton Style="{StaticResource LinkStyle}" x:Name="vSubLink2" Content="SubLink2" Margin="0,0,25,0"/>
            <RadioButton Style="{StaticResource LinkStyle}" x:Name="vSubLink3"     Content="SubLink3"     Margin="0,0,25,0"/>

        </StackPanel>
        <Grid Grid.Row="1" Margin="0,10,0,0">
        <MyApp:SubLink1 Visibility="{Binding ElementName=vSubLink1, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>
        <MyApp:SubLink2 Visibility="{Binding ElementName=vSubLink2, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>
        <MyApp:SubLink3 Visibility="{Binding ElementName=vSubLink3, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>
        </Grid>
    </Grid>

Could you please tell me how to solve my problem?你能告诉我如何解决我的问题吗?

The element name is wrong in the binding!绑定中元素名称错误! Try the code changes below and revert if any issues.尝试下面的代码更改,如果有任何问题,请恢复。

<MyApp:Link1 Visibility="{Binding ElementName=VLink1, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>
<MyApp:Link2 Visibility="{Binding ElementName=VLink2, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>
<MyApp:Link3 Visibility="{Binding ElementName=VLink3, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>
<MyApp:Link4 Visibility="{Binding ElementName=VLink4, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"/>

Could you please tell me how to solve my problem?你能告诉我如何解决我的问题吗?

As an aside...you don't necessarily have to use a converter.顺便说一句……您不一定必须使用转换器。 One could bind directly to a visibility property, either exposed on the view or if you are using MVVM the VM.可以直接绑定到可见性属性,或者在视图上公开,或者如果您使用的是 MVVM,则为 VM。 Such as where I do specific logic on my VM:例如我在 VM 上执行特定逻辑的位置:

public Visibility IsSendAvailable
{
    get { return CanSend ? Visibility.Visible : Visibility.Collapsed; }
    set { _IsSendAvailable = value; OnPropertyChanged("IsSendAvailable"); }
}

Then bind in your Xaml such as然后绑定在您的 Xaml 中,例如

<MyApp:SubLink1 Visibility="{Binding IsSendAvailable}"/>

assuming that the page's datacontext is set to a VM or a View with a property IsSendAvailable .假设页面的 datacontext设置为 VM 或具有IsSendAvailable属性的IsSendAvailable


Note how it used INotifyPropertyChange OnPropertyChanged method to announce the change to the world.请注意它如何使用INotifyPropertyChange OnPropertyChanged方法向世界宣布更改。 I provide an MVVM overview on my blog:我在我的博客上提供了 MVVM 概述:

Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding Xaml:ViewModel 主页实例化和加载策略,以便于绑定

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

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