UWP绑定到ConverterParameter无效

时间:2018-08-21 23:47:38

标签: c# xaml uwp

我将VM属性绑定到我的ConverterParemeter,但是在Converter中它总是显示为null,是否还有其他方法可以将属性传递给转换器。

由于我无法共享原始代码,因此下面是我面临的问题的副本。即使设置了FilterType值,在我的DummyConverter中参数始终为null。

Xaml:

<Grid>
        <ComboBox x:Name="myComboBox" ItemsSource="{Binding ComboBoxList, Converter={StaticResource DummyConverter}, ConverterParameter={Binding FilterType}}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="50" Width="150">
        </ComboBox>
    </Grid>

VM:

public class MainViewModel : INotifyPropertyChanged
    {
        private string header;

        public string Header
        {
            get { return header; }
            set
            {
                header = value;
                RaisePropertyChange(nameof(Header));
            }
        }

        private Person selectedPerson;

        public Person SelectedPerson
        {
            get { return selectedPerson; }
            set { selectedPerson = value; RaisePropertyChange(nameof(SelectedPerson)); }
        }


        private ObservableCollection<Person> comboBoxList;

        public ObservableCollection<Person> ComboBoxList
        {
            get { return comboBoxList; }
            set { comboBoxList = value; }
        }

        public FilterType FilterType { get; set; }

        public DelegateCommand DropDownClosedCommand { get; set; }

        public MainViewModel()
        {
            Header = "My Header";

            FilterType = FilterType.None;

            ComboBoxList = new ObservableCollection<Person> {
                 new Person() { Name = "Person 1", IsChecked = false },
                      new Person() { Name = "Person 2", IsChecked = false },
                      new Person() { Name = "Person 3", IsChecked = false },
                      new Person() { Name = "Person 4", IsChecked = false }
            };

            DropDownClosedCommand = new DelegateCommand(OnDropDownClosed);

        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChange(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void OnDropDownClosed(object e)
        {
            //CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            //() =>
            //{
                SelectedPerson = ComboBoxList.FirstOrDefault(x => x.IsChecked);
            //});
        }
    }

转换器:

public class DummyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    }

在WPF中,我可以使用MultiBinding,但是在UWP中,MultiBinding不可用。

编辑:

this博客中,我发现获取空值的原因是“原因在于ConverterParameter不是依赖属性,而是“简单”对象。” < / p>

因此,下面是修改后的代码:

Xaml:

<Page.Resources>
        <converters:DummyConverter x:Name="DummyConverter" FilterType="{Binding FilterType}"/>
</Page.Resources>

<Grid>
         <ComboBox x:Name="myComboBox" ItemsSource="{Binding ComboBoxList, Converter={StaticResource DummyConverter}}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="50" Width="150">
          <interactivity:Interaction.Behaviors>
            <core:DataTriggerBehavior Binding="{Binding IsDropDownOpen, ElementName=myComboBox}" ComparisonCondition="NotEqual" Value="True">
                <core:InvokeCommandAction Command="{Binding DropDownClosedCommand}"/>
            </core:DataTriggerBehavior>
          </interactivity:Interaction.Behaviors>
    </ComboBox>
    </Grid>

转换器:

  public class DummyConverter : DependencyObject, IValueConverter
    {
        public FilterType FilterType
        {
            get { return (FilterType)GetValue(FilterTypeProperty); }
            set { SetValue(FilterTypeProperty, value); }
        }

        public static readonly DependencyProperty FilterTypeProperty =
        DependencyProperty.Register("FilterType",
                                    typeof(FilterType),
                                    typeof(DummyConverter),
                                    new PropertyMetadata(null));

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    }

MainViewModel.cs

private void OnDropDownClosed(object e)
        {
            //CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            //() =>
            //{
                SelectedPerson = ComboBoxList.FirstOrDefault(x => x.IsChecked);
                FilterType = FilterType.Descending;
                this.RaisePropertyChange(nameof(ComboBoxList));
            //});
        }

我正在OnDropDownClosed中更改FilterType的值,但在转换器中不受影响。

1 个答案:

答案 0 :(得分:1)

我发现了FilterType不变的原因,这是因为PropertyChangedEvent没有触发。我更新了以下代码,现在可以按预期运行。

private void OnDropDownClosed(object e)
{
   SelectedPerson = ComboBoxList.FirstOrDefault(x => x.IsChecked);
   FilterType = FilterType.Descending;
   this.RaisePropertyChange(nameof(FilterType));
   this.RaisePropertyChange(nameof(ComboBoxList));
 }