3

I have a problem attempting to follow the advice in: How to bind inverse boolean properties in WPF?

When I use with ResourceDictionary, it give run-time error. InverseBooleanConverter not found.

XMAL as follows:

<UserControl x:Class="SMTF.MasterDataView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SMTF" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="466" d:DesignWidth="483">
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../AppResource.xaml" />
            <ResourceDictionary Source="../DefaultStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
    <Grid>
    <StackPanel  HorizontalAlignment="Left" Margin="200,12,0,0" Name="stkMain" VerticalAlignment="Top" >
        <Grid Margin="4">
            <ContentControl Visibility="{Binding IsChecked, ElementName=VisibilityToggle, Converter={StaticResource InverseBooleanConverter}}" >
                <Border Grid.Column="2" Style="{StaticResource MainBorderStyle}">
                    <HeaderedContentControl   Content="{Binding Path=WorkspaceView}" ContentTemplate="{StaticResource WorkspacesTemplate}" Header="View" Style="{StaticResource MainHCCStyle}"/>
                </Border>
            </ContentControl>
        </Grid>
        <Grid DockPanel.Dock="Bottom" Margin="0,2,4,2">
            <TextBlock HorizontalAlignment="Right">

               <ToggleButton  x:Name="VisibilityToggle" Focusable="False" Style="{StaticResource SMToggle}"  Command ="{Binding ShowNew}" >

                </ToggleButton>
               <!--<ToggleButton x:Name="VisibilityToggle"   Background="Transparent" Command ="{Binding ShowNew}" >
                     <Image Source="/Image/Add.png"  Width="24" />
                </ToggleButton>-->
            </TextBlock>

        </Grid>

        <Grid Margin="4">
            <ContentControl Visibility="{Binding IsChecked, ElementName=VisibilityToggle, Converter={StaticResource BoolToVisibility}}" >
            <Border Grid.Column="2" Style="{StaticResource MainBorderStyle}">
                <HeaderedContentControl  Content="{Binding Path=WorkspaceEdit}" ContentTemplate="{StaticResource WorkspacesTemplate}" Header="Configure" Style="{StaticResource MainHCCStyle}"/>
            </Border>
            </ContentControl>
        </Grid>
    </StackPanel>

</Grid>
</UserControl>

I'm using the same code provided in the link. ie:

[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBooleanConverter: IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        if (targetType != typeof(bool))
            throw new InvalidOperationException("The target must be a boolean");

        return !(bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    #endregion
}

in the AppResource XML

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:vm="clr-namespace:SMTF">
 <vm:InverseBooleanConverter x:Key="InverseBoolToVisibility" /> 
 .....
 .....
</ResourceDictionary>

Thanks in advance NS

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
SNS
  • 395
  • 4
  • 8
  • 19

2 Answers2

5

An alternative to converters for style related binding is to use Style.Triggers, the following shows a canvas when checkbox IsChecked = false, which would otherwise require an InverseBooleanConverter.

<Canvas x:Name="Overlay">
    <Canvas.Style>
        <Style TargetType="Canvas">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=MyCheckbox}" Value="True">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
                <DataTrigger Binding="{Binding IsChecked, ElementName=MyCheckbox}" Value="False">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>                                
            </Style.Triggers>
        </Style>
    </Canvas.Style>
    <Rectangle Canvas.ZIndex="3" Fill="#99333333" Height="25" Stroke="Transparent" Width="293" Canvas.Left="10" Canvas.Top="-25"/>
</Canvas>
jv_
  • 1,784
  • 1
  • 16
  • 12
4

The key you are using is not correct. You resource key is InverseBoolToVisibility, while you have used InverseBooleanConverter as key.

Change the resource key to refer to correct resource as

<ContentControl Visibility="{Binding IsChecked, ElementName=VisibilityToggle, Converter={StaticResource InverseBoolToVisibility}}" >

Also your implementation for convereter is wrong. If you want to change the Visibility based on Boolean inverse value update your converter code as:

public class InverseBooleanConverter: IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        if (targetType != typeof(Visibility))
            throw new InvalidOperationException("The target must be a boolean");

        if(!(bool)value)
        {
            return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    #endregion
}
Nitin
  • 18,344
  • 2
  • 36
  • 53
  • thanks. Is there anything wrong with converter class? or any addtional code I have to use? I'm getting the targetType not boolean error! – SNS Oct 15 '13 at 08:40
  • and what if the xaml of the answer is in a usercontrol and the convert in the mainWindowViewModel? How can one see the other? II tried with ancestor type, but it doesn't work – Piero Alberto Sep 23 '14 at 14:13