为ItemsControl调用ConvertBack时?

时间:2012-12-29 15:09:48

标签: c# wpf xaml itemscontrol updatesourcetrigger

我有以下项目数据类,以及转换器。

class ListBoxViewItem
{
    public String Name { get; set; }
    public Boolean IsChecked { get; set; }
}

[ValueConversion(typeof(List<String>),typeof(List<ListBoxViewItem>))]
class ListToItemConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return null;
        IEnumerable<String> l = value as IEnumerable<String>;
        return (from n in l select new ListBoxViewItem() { IsChecked = true, Name = n });
    }

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

class ListBoxData
{
    public List<String> AllData
    {
        get
        {
            return new List<string>()
            {
                "FOO",
                "BAR"
            };
        }
        set
        {

        }
    }
}

我将ListBoxData的实例绑定到列表框控件的ItemsSource。如下:

    <ListBox>
        <ListBox.ItemsSource>
            <Binding>
                <Binding.Path>AllData</Binding.Path>
                <Binding.Converter>
                    <local:ListToItemConverter />
                </Binding.Converter>
                <Binding.Mode>TwoWay</Binding.Mode>
            </Binding>
        </ListBox.ItemsSource>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}"
                          Content="{Binding Name}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

所以我的问题是,当列表框显示时调用Convert函数,但是因为我使用TwoWay绑定绑定实例和列表框,所以此列表框中的每个项都是一个复选框,但是当我选中/取消选中此列表框中的复选框时,不会调用ConvertBack

我不确定ConvertBack是否符合我的预期。但如果我想在检查状态发生变化时可以触发ConvertBack,我该怎么办?

3 个答案:

答案 0 :(得分:1)

当转换器在模式= TwoWay的绑定中使用时,将触发ConvertBack方法。 例如,它用于绑定到TextBox的Text属性。 当在TextBox中显示值时值将更改时将触发“Convert”方法,并且当用户更改TextBox值时将触发“ConvertBack”。

答案 1 :(得分:0)

使用ConvertBack函数的反向功能将UpdateSourceTrigger添加到绑定和实现Convert函数。

更改以下

<CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}">

 <CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">

ConvertBack功能如下

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (value is IEnumerable<ListBoxViewItem>) 
    {
      var l = (IEnumerable<ListBoxViewItem>)value;
      return l.Select(l.IsChecked);
    }

   return null;
}

答案 2 :(得分:0)

您正在转换ListBox.ItemsSource,而不是CheckBox.IsChecked

我认为ListBox.ItemsSource甚至不会使用TwoWay绑定,因为默认情况下ListBoxes不会更改其ItemsSource的源集合。

如果您希望在更改CheckBox.IsChecked时运行转换器,则需要指定要在IsChecked绑定中使用的转换器

<CheckBox IsChecked="{Binding IsChecked, 
                              Converter={StaticResource MyCheckboxConverter}}"
          Content="{Binding Name}"/>

但是在这种情况下,不需要转换器,因为IsChecked已经是布尔值,所以你根本不需要转换它。

如果您在CheckBox.IsChecked更改时尝试触发某些内容,最好确保IsChecked触发PropertyChange通知,并执行PropertyChanged中的代码包含IsChecked属性的类的事件。

相关问题