2013-10-15 52 views
2

我有一個問題。 How to bind inverse boolean properties in WPF?如何在WPF中綁定反布爾屬性與如何綁定反布爾屬性在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

+0

請分享您嘗試過的代碼/ xaml。如何定義轉換器以及如何引用它 – Nitin

+0

@nit我已添加請求的信息 – SNS

回答

3

您所使用的密鑰是不正確的。您的資源關鍵是InverseBoolToVisibility,而您使用InverseBooleanConverter作爲關鍵。

變化的關鍵資源是指正確的資源作爲

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

還實施以供convereter是錯誤的。如果你想改變能見度基於布爾相反值更新您的轉換代碼:

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 
} 
+0

謝謝。轉換器類有什麼問題嗎?或我必須使用的任何附加代碼? 我得到的targetType不是布爾錯誤! – SNS

+0

您的目標類型是可見性..更新了答案.. – Nitin

+0

以及如果答案的xaml在用戶控件中,並且在mainWindowViewModel中進行了轉換,該怎麼辦?如何能看到對方?我試圖用祖先類型,但它不起作用 –

3

到轉換器相關的結合風格的另一種方法是使用Style.Triggers,下面顯示畫布時複選框=器isChecked假,否則將需要一個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>