IValueConverter 执行次数超出预期
我有一个转换器,它从构成 DataGridCell 的 TextBlock 中获取文本,并将其转换为红色或黑色画笔,具体取决于值是负数还是正数。但是,转换器执行的次数多于网格数据源中的项目数。例如,如果我只是绑定一个包含 1 个对象的集合,则转换器会执行 2 次。第一次 value
参数是空字符串,第二次它实际上包含我期望的值。如果我向列表中添加更多对象,初始“空”执行的数量就会增加。我做错了什么?
<Window.Resources>
<conv:NumericValueBrushColorConverter x:Key="NumericValueBrushColorConverter"></conv:NumericValueBrushColorConverter>
</Window.Resources>
<DataGrid ItemsSource="{Binding CashReport}" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Beginning Cash Available" Binding="{Binding BeginningBalance, StringFormat={}{0:C}}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Foreground" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Text, Converter={StaticResource NumericValueBrushColorConverter}}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
[ValueConversion(typeof(string), typeof(SolidColorBrush))]
internal class NumericValueBrushColorConverter : IValueConverter
{
static int i = 0;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string strValue = value as string;
i++;
if(string.IsNullOrEmpty(strValue)) return new SolidColorBrush(Colors.Black);
if (strValue.StartsWith("("))
{
return new SolidColorBrush(Colors.Red);
}
else
{
return new SolidColorBrush(Colors.Black);
}
}
...
}
I have a converter that takes the text from a TextBlock that makes up a DataGridCell and converts it to a red or black brush depending if the value is negative or positive. However the converter executes more times than there are items in the datasource of the grid. For example if I simply bind a collection that contains 1 object, the converter executes 2 times. The first time the value
parameter is an empty string, the second time it actually contains the value that I would expect. If I add more objects to the list, the number of initial "empty" executions grows. What am I doing wrong?
<Window.Resources>
<conv:NumericValueBrushColorConverter x:Key="NumericValueBrushColorConverter"></conv:NumericValueBrushColorConverter>
</Window.Resources>
<DataGrid ItemsSource="{Binding CashReport}" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Beginning Cash Available" Binding="{Binding BeginningBalance, StringFormat={}{0:C}}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Foreground" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Text, Converter={StaticResource NumericValueBrushColorConverter}}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
[ValueConversion(typeof(string), typeof(SolidColorBrush))]
internal class NumericValueBrushColorConverter : IValueConverter
{
static int i = 0;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string strValue = value as string;
i++;
if(string.IsNullOrEmpty(strValue)) return new SolidColorBrush(Colors.Black);
if (strValue.StartsWith("("))
{
return new SolidColorBrush(Colors.Red);
}
else
{
return new SolidColorBrush(Colors.Black);
}
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,网格行中的 TextBlock 被创建,因此为空(= 第一次执行),然后内容绑定到当前项目的值(= 第二次执行)。这里没什么问题。
Well, first the TextBlock in the grid row is created and thus empty (= first execution) and then the content is bound to the value of the current item (= second execution). Nothing wrong here.
我希望@Daniel Hilgarth 是对的。在尝试转换之前,您应该检查转换器中的值。当您不希望绑定运行时,可以返回 Binding.DoNothing。
I expect @Daniel Hilgarth is right. You should check the value in your converter before trying to convert. You can return Binding.DoNothing when you don't want the binding to run.