WPF IMultiValueConverter DependencyProperty.UnsetValue

时间:2018-05-06 06:52:51

标签: c# wpf data-binding imultivalueconverter

我有几个领域 对于这些字段,我希望列表中的某些行可见或不可见 根据这些领域。

我正在尝试使用IMultiValueConverter,但是,我得到的所有值都是{DependencyProperty.UnsetValue}。

我附上我的代码希望得到帮助 谢谢你的回答。

XAML:

<ListView x:Name="LVGuiCoreBus" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" MouseDown="LVGui_MouseDown"  Grid.Row="1" >               
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.Visibility>
                    <MultiBinding Converter="{StaticResource MultiValueConvertorVisibility}" UpdateSourceTrigger="PropertyChanged">
                        <Binding ElementName="Source" Path="Visibility" UpdateSourceTrigger="PropertyChanged"/>
                        <Binding ElementName="Destination" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"/>
                        <Binding ElementName="Module" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"/>
                        <Binding ElementName="Controll" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"/>
                        <Binding ElementName="Command" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"/>
                        <Binding ElementName="HSCommand" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"/>
                    </MultiBinding>
                </Grid.Visibility>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="130"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="10"/>                                                      
                    <ColumnDefinition Width="70"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding DateNTimeStr}"  Foreground="Green" Grid.Column="0" />
                <TextBlock Text="{Binding Source}" Foreground="{Binding Source,Converter={StaticResource CoreBusPanelModuleColorConverter}}" Grid.Column="2" Background="{ Binding Source, Converter={StaticResource BackGroundFilterConverterBySource}}" />
                <TextBlock Text="{Binding Destination}" Foreground="{Binding Destination,Converter={StaticResource CoreBusPanelModuleColorConverter}}" Grid.Column="4" Background="{ Binding Destination, Converter={StaticResource BackGroundFilterConvertorByDestantation}}"  />
                <TextBlock Text="{Binding Module}" Grid.Column="6" HorizontalAlignment="Center" Background="{ Binding Module, Converter={StaticResource BackGroundFilterByModule}}"   />
                <TextBlock Text="{Binding Controll}" Grid.Column="8"  Background="{ Binding Controll, Converter={StaticResource BackGRoundFilterByControll}}"  />
                <TextBlock Text="{Binding Command}" Grid.Column="10" HorizontalAlignment="Center"  Background="{ Binding Command, Converter={StaticResource BackGroundFilterByCommand}}"  />
                <TextBlock Text="{Binding HSCommand}" Grid.Column="12" HorizontalAlignment="Center"  Background="{ Binding HSCommand, Converter={StaticResource BackGroundFilterByHsCommand}}"   />
                <TextBlock Text="{Binding Data_Str}"  Grid.Column="14"/>                                                                
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ContextMenu>
        <ContextMenu x:Name="CMMenuCopy">
            <MenuItem x:Name="MCICopyLine" Header="Copy Line" Click="MCICopyLine_Click" />
            <MenuItem x:Name="MCICopyText" Header="Copy Only Data Array" Click="MCICopyText_Click" />
            <MenuItem x:Name="MCIClear" Header="Clear" Click="MCIClear_Click"/>
        </ContextMenu>
    </ListView.ContextMenu>
</ListView>

转换器实施:

public class MultiValueConvertorVisibility : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool x = System.Convert.ToBoolean(values[0]);
        if (x)
        {
            return System.Windows.Visibility.Visible;
        }
        else
        {
            return System.Windows.Visibility.Collapsed;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
  }

C#代码

 ObservableCollection<CStrucutreData> _LStructureGUIFile = new   
 ObservableCollection<CStrucutreData>();
 LVGuiCoreBus.ItemsSource = _LStructureGUI;
 LVGuiCoreBus.DataContext = _LStructureGUI;                        

C#CStrucutreData:

public class CStrucutreData : INotifyPropertyChanged
{


    // This method is called by the Set accessor of each property.
    // The CallerMemberName attribute that is applied to the optional propertyName
    // parameter causes the property name of the caller to be substituted as an argument.
    private void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


    private string _DateNTimeStr;

    /// <summary>
    /// Date Time in a string
    /// </summary>
    public string DateNTimeStr
    {
        get
        {
            return _DateNTimeStr;
        }

        set
        {
            _DateNTimeStr = value;
            OnPropertyChanged("DateNTimeStr");
        }
    }


    private DateTime _DateNTime;

    /// <summary>
    /// DateTime in DateTime Format
    /// </summary>
    public DateTime DateNTime
    {
        get
        {
            return _DateNTime;
        }

        set
        {
            _DateNTime = value;
            OnPropertyChanged("DateNTime");
        }
    }




    private byte [] _Data_byte;


    /// <summary>
    /// Data in  bytes array
    /// </summary>
    public byte[] Data_byte
    {
        get
        {
            return _Data_byte;
        }

        set
        {
            _Data_byte = value;
            OnPropertyChanged("Data_byte");
        }
    }



    private string _Data_Str;

    /// <summary>
    /// Data in string 
    /// </summary>
    public string Data_Str
    {
        get
        {
            return _Data_Str;
        }

        set
        {
            _Data_Str = value;
            OnPropertyChanged("Data_Str");
        }
    }


    private string _Translation;

    /// <summary>
    /// a small translation to translate Command
    /// </summary>
    public string Translation
    {
        get
        {
            return _Translation;
        }

        set
        {
            _Translation = value;
            OnPropertyChanged("Translation");
        }
    }


    private string _Channel;

    /// <summary>
    /// a small translation to translate Command
    /// </summary>
    public string Channel
    {
        get
        {
            return _Channel;
        }

        set
        {
            _Channel = value;
            OnPropertyChanged("Channel");
        }
    }


    private string _Source;

    /// <summary>
    /// Date Time in a string
    /// </summary>
    public string Source
    {
        get
        {
            return _Source;
        }

        set
        {
            _Source = value;
            OnPropertyChanged("Source");
        }
    }

    private string _Destination;

    /// <summary>
    /// Date Time in a string
    /// </summary>
    public string Destination
    {
        get
        {
            return _Destination;
        }

        set
        {
            _Destination = value;
            OnPropertyChanged("Destination");
        }
    }

    private string _Module;

    /// <summary>
    /// Date Time in a string
    /// </summary>
    public string Module
    {
        get
        {
            return _Module;
        }

        set
        {
            _Module = value;
            OnPropertyChanged("Module");
        }
    }

    private string _Controll;

    /// <summary>
    /// Date Time in a string
    /// </summary>
    public string Controll
    {
        get
        {
            return _Controll;
        }

        set
        {
            _Controll = value;
            OnPropertyChanged("Controll");
        }
    }


    private string _Command;

    /// <summary>
    /// Date Time in a string
    /// </summary>
    public string Command
    {
        get
        {
            return _Command;
        }

        set
        {
            _Command = value;
            OnPropertyChanged("Command");
        }
    }

    private string _HSCommand;

    /// <summary>
    /// Date Time in a string
    /// </summary>
    public string HSCommand
    {
        get
        {
            return _HSCommand;
        }

        set
        {
            _HSCommand = value;
            OnPropertyChanged("HSCommand");
        }
    }

    /// <summary>
    /// init all variables for properties
    /// </summary>
    public CStrucutreData()
    {
        _Data_byte = new byte[1];
        _Data_Str = "";
        _DateNTime = new DateTime();
        _DateNTimeStr = "";
        _Translation = "";
        _Channel = "";
        _Source = "";
        _Destination = "";
        _Module = "";
        _Controll = "";
        _Command = "";
        _HSCommand = "";
    }


    public event PropertyChangedEventHandler PropertyChanged;



}

0 个答案:

没有答案