【发布时间】:2020-03-13 21:39:23
【问题描述】:
我有一个绑定到值列表的 ComboBox,为了这个例子,一个简单的 ints 数组。该控件分配了一个自定义ItemTemplate 来格式化其元素,而后者又使用IValueConverter 进行某些操作。
有一个失败的特殊情况:如果ComboBox 选择了一个元素,并且项目列表发生更改,则转换器的Convert 使用空字符串调用,这绝对不是绑定的值之一在我的控制之下。 (请注意,我不是在谈论ConvertBack 方法。)
我的问题是:
- 为什么在没有字符串绑定到控件时,
value是空字符串 ("") 被调用? - 有哪些非hacky的解决方案? (我可以将
if (value is "") return null;放在转换器中,它似乎使错误消失了,但我觉得它正在治疗症状,而不是原因。)
可以使用仅包含这 3 个文件的简单 WPF 项目 (dotnet new wpf) 重现该问题(该问题在 .NET 的 Framework 和 Core 版本中都存在):
MainWindow.xaml:
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ComboBox x:Name="Selector" VerticalAlignment="Center" HorizontalAlignment="Center" Width="100">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={x:Static local:Converter.Instance}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button HorizontalAlignment="Right" VerticalAlignment="Bottom" Content="Replace" Click="Button_Click" />
</Grid>
</Window>
MainWindow.xaml.cs:
using System;
using System.Windows;
namespace test
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Selector.ItemsSource = new int[] { 1, 2, 3 };
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Selector.ItemsSource = new int[] { -1, -2, -3 };
}
[STAThread]
public static void Main(String[] args)
{
new MainWindow().ShowDialog();
}
}
}
最后是 Converter.cs:
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Data;
namespace test
{
class Converter : IValueConverter
{
public static Converter Instance { get; } = new Converter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Debug.Assert(value is int);
return (2 * (int) value).ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
}
这 3 个文件构成了展示此行为的最小示例。如果单击“替换”按钮没有首先从组合框中选择任何内容,则程序运行正常。但是,如果在单击按钮之前在 ComboBox 中选择了任何值,则转换器中的断言将失败,因为单击按钮时,转换器将传递 value 或 ""。
【问题讨论】: