wpf - 运行时错误 : InverseBooleanConverter not found

标签 wpf styles

我在尝试遵循以下建议时遇到问题: How to bind inverse boolean properties in WPF?

当我与 ResourceDictionary 一起使用时,它会给出运行时错误。未找到 InverseBooleanConverter。

XMAL如下:

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

我使用的是链接中提供的相同代码。 即:

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

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

提前致谢 NS

最佳答案

样式相关绑定(bind)的转换器的替代方法是使用 Style.Triggers,下面显示了复选框 IsChecked = false 时的 Canvas ,否则需要 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>

关于wpf - 运行时错误 : InverseBooleanConverter not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19374814/

相关文章:

css - 是否可以制作具有多个边框和阴影的按钮?

css - 为 nopcommerce 编辑和创建主题的最佳方式是什么?

c# - WinForms 中的 WPF ElementHost 不接收鼠标点击

c# - 绑定(bind) DataGridComboBoxColumn SelectedIndex

c# - 必须从 WPF 中的日历中单击两次

css - 根据 php 条件给出样式

android - 在 Android 操作栏中设置 SearchView 的样式

wpf - 如何将 WPF 的 D3DImage 与托管 DirectX 或 XNA 一起使用?

c# - 使用 WPF 中文本框的 KeyDown 事件捕获 Ctrl-X

java - windowbackground 在深色和浅色主题中不起作用