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

时间:2016-04-29 14:27:49

标签: c# wpf xaml

我对WPF很陌生。我在ToggleSwitch上编写了一个基于Demo的小型应用程序:https://toggleswitch.codeplex.com/

基本上有4个单选按钮与4个xaml文件绑定(就像我在上面写的链接的Demo中一样)。我们称之为Base1.xml,Base2.xml等。

然后我想为其中一个文件做同样的事情。 我在3个xamls中放入Base2.xml 3个单选按钮 - Sub1.xml Sub2.xml Sub3.xml。我复制了逻辑然后我遇到了问题。 其中一个Subx.xml始终可见。

我认为这是可见/崩溃的问题,它应该是可见的/隐藏的,所以我尝试了从这个链接的解决方案: http://www.rhyous.com/2011/02/22/binding-visibility-to-a-bool-value-in-wpf/

但它不起作用......

这是我的主要代码:

<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>

这是我的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>

你能告诉我如何解决我的问题吗?

2 个答案:

答案 0 :(得分:1)

绑定中的元素名称错误!尝试下面的代码更改,如果有任何问题,请还原。

<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}}"/>

答案 1 :(得分:0)

  

你能告诉我如何解决我的问题吗?

顺便说一下......你不一定要使用转换器。可以直接绑定到可见性属性,可以在视图上公开,也可以将MVVM用于VM。比如我在VM上执行特定逻辑的地方:

GetComponent<Rigidbody2D>().velocity = new Vector3 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

然后绑定你的Xaml,如

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

假设页面的datacontext 设置为VM或具有属性<MyApp:SubLink1 Visibility="{Binding IsSendAvailable}"/> 的视图。

请注意它是如何使用IsSendAvailable INotifyPropertyChange方法宣布对世界的更改。我在我的博客上提供MVVM概述:

Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding