WPF MultiBinding 实现控件 Tooltip 多绑定

//convert 

public class CanBeAppliedDisplayConverter : IMultiValueConverter

    {
        #region Implementation of IMultiValueConverter
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values[0].ToString() == string.Empty || values[1].ToString() == string.Empty)
                return "";
            return values[0].ToString() + values[1].ToString();
        }


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

    }

//viewmodel

public class viewmodel : INotifyPropertyChanged
    {


        private string _name1;
        public string name1
        {
            get { return _name1; }
            set
            {
                _name1 = value;
                OnPropertyChanged("name1");
            }
        }


        private string _name2;
        public string name2
        {
            get { return _name2; }
            set
            {
                _name2 = value;
                OnPropertyChanged("name2");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;


        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
            
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }


//xaml

  <TextBlock DataContext="{Binding}">
             <TextBlock.ToolTip>
                <ToolTip >
                    <TextBlock DataContext="{Binding}">
                        <TextBlock.Text>
                            <MultiBinding Converter="{StaticResource CanBeAppliedDisplayConverter}">
                                <Binding   Path="name1" />
                                <Binding   Path="name2" />
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </ToolTip>
            </TextBlock.ToolTip>
            </TextBlock>

Guess you like

Origin blog.csdn.net/wangyue4/article/details/9746463