简体   繁体   English

可见性转换器绑定不起作用

[英]Visibility Converter Binding Not Working

I have a visibility converter for aa DataGrid that should hide the grid when the item source for the grid is null. 我有一个DataGrid的可见性转换器,当网格的项目源为空时,它应该隐藏网格。 The item source is a property of the class for the window. item source是窗口类的属性。

Here is partial XAML for the window - the window and visibility converter definition and the data grid: 这是窗口的部分XAML - 窗口和可见性转换器定义和数据网格:

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. 以下是它应该绑定到的属性和VisibilityConverter的代码。 the idea is that if the PortList is null - it will be by default - the DataGrid should stay collapsed. 我的想法是,如果PortList为null - 默认情况下 - DataGrid应该保持折叠状态。 I have verified that the PortList is null when I want it to be. 我已经验证了PortList在我想要的时候是null。

 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 我已经尝试将XAML更改为此绑定

Visibility="{Binding PortList, Converter={StaticResource PortVisConverter}}"

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. 然后,如果要将对该属性的更改传播到UI,则需要使用INotifyPropertyChanged

As an aside, you could have figured this out if you looked at the output window while running and saw System.Data exceptions. 顺便说一句,如果你在运行时查看输出窗口并看到System.Data异常,你可能已经想到了这一点。 Easiest way to debug binding issues :) 调试绑定问题的最简单方法:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM