【问题标题】:When ConvertBack is called for ItemsControl?何时为 ItemsControl 调用 ConvertBack?
【发布时间】:2012-12-29 15:09:47
【问题描述】:

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

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 绑定来绑定实例和列表框,但@987654328当我选中/取消选中此列表框中的复选框时,不会调用 @。

我不确定ConvertBack 是否可以按我的预期工作。但是如果我希望ConvertBack可以在检查状态更改时触发,我该怎么办?

【问题讨论】:

标签: c# wpf xaml itemscontrol updatesourcetrigger


【解决方案1】:

当在 Mode=TwoWay 的绑定中使用 Converter 时,将触发 ConvertBack 方法。 例如,它用于绑定到 TextBox 的 Text 属性。 在 TextBox 中显示值时会触发“Convert”方法,当用户更改 TextBox 值时会触发“ConvertBack”。

【讨论】:

    【解决方案2】:

    UpdateSourceTrigger 添加到绑定并实现ConvertBack 函数,具有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;
    }
    

    【讨论】:

    • 我在一个WPF应用程序中尝试,并添加UpdateSourceTrigger,ConvertBack中的断点在调试模式下从未触发。
    【解决方案3】:

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

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

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

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

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

    如果您试图在您的CheckBox.IsChecked 更改时触发某些事情,您最好确保IsChecked 触发PropertyChange 通知,并在包含@ 的类的PropertyChanged 事件中执行代码987654333@财产。

    【讨论】:

      猜你喜欢
      • 2019-12-25
      • 2011-07-21
      • 2012-08-19
      • 2011-09-24
      • 2013-01-03
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      相关资源
      最近更新 更多