Reputation: 1234
I have a visibility converter for a a DataGrid that should hide the grid when the item source for the grid is null. The item source is a property of the class for the window.
Here is partial XAML for the window - the window and visibility converter definition and the data grid:
Window:
<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"/>
Datagrid:
<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">
Here is the code behind for the property it should bind to and the VisibilityConverter. the idea is that if the PortList is null - it will be by default - the DataGrid should stay collapsed. I have verified that the PortList is null when I want it to be.
public partial class FabricDiagram : Window
{
public List<PortResult> PortList = null;
lastly the visibilityconverter. I have verified in the debugger that it is not getting called.
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();
}
}
I've tried changing the XAML to this binding
Visibility="{Binding PortList, Converter={StaticResource PortVisConverter}}"
Upvotes: 2
Views: 1442
Reputation: 61379
Your binding is failing, so the converter never runs.
public List<PortResult> PortList = null;
declares a field and you can only bind to properties. Changing to:
public List<PortResult> PortList { get; set; } = null;
will solve your first problem; then you need to use INotifyPropertyChanged
if you want changes to that property to propagate to the UI.
As an aside, you could have figured this out if you looked at the output window while running and saw System.Data
exceptions. Easiest way to debug binding issues :)
Upvotes: 3