1
我有一個DataGrid的可見性轉換器,當網格的item源爲null時應該隱藏網格。項目源是窗口類的屬性。可見性轉換器綁定不起作用
下面是窗口偏XAML - 窗口和可視性轉換器定義和數據網格:
窗口:
<Window x:Name="DiagramWindow"
x:Class="FabricAnalyzer.FabricDiagram"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FabricAnalyzer"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
mc:Ignorable="d"
Title="FabricDiagram"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<ResourceDictionary>
<local:SwitchThumbColorConverter x:Key="SwitchThumbColor"/>
<local:PortThumbColorConverter x:Key="PortThumbColor"/>
<local:StringLengthVisiblityConverter x:Key="VisConverter"/>
<local:PortListVisiblityConverter x:Key="PortVisConverter"/>
數據網格:
<Grid Name="FabricGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<DataGrid Name="SVCPortDataGrid" Grid.Column="0" Width="Auto"
Visibility="{Binding Path=PortList, Converter=
{StaticResource PortVisConverter}}"
AutoGenerateColumns="False">
這裏是後面的代碼爲它應該綁定的屬性和VisibilityConverter。這個想法是,如果PortList爲空 - 它將默認 - DataGrid應該保持摺疊狀態。我已經驗證過,當我想要的時候,PortList是空的。
public partial class FabricDiagram : Window
{
public List<PortResult> PortList = null;
最後是visibilityconverter。我在調試器中證實它沒有被調用。
public class PortListVisiblityConverter : IValueConverter
{
public Object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return Visibility.Collapsed;
}
else
{
return Visibility.Visible;
}
}
public Object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我試圖改變XAML此綁定
Visibility="{Binding PortList, Converter={StaticResource PortVisConverter}}"