【发布时间】:2016-10-07 13:11:53
【问题描述】:
我有一个Window 类,其中我有几个TextBlock 元素,它们应该通过Binding 属性的值接收Background 颜色。第一个“转换器绑定”工作正常,并完成了预期的一切。今天我尝试用另一个 Converter 实现另一个“转换器绑定”,但它不起作用:
(我省略了ConvertBack 方法,因为这里不需要它们):
namespace InsightTool.Gui.Helper {
[ValueConversion(typeof(double), typeof(Brush))]
public class AverageExecutionTimeToColorConverter : IValueConverter {
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
double val;
double.TryParse(value.ToString(), out val);
if (val >= 10000) {
return Brushes.Red;
} else if (val >= 5000) {
return Brushes.Orange;
} else {
return Brushes.Green;
}
}
}
[ValueConversion(typeof(int), typeof(Brush))]
public class ThreadsAvailableCountToColorConverter : IValueConverter {
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
int val;
int.TryParse(value.ToString(), out val);
if (val < 100) {
return Brushes.Red;
} else if (val < 200) {
return Brushes.Orange;
} else if (val < 500) {
return Brushes.Yellow;
} else {
return Brushes.Green;
}
}
}
}
在Window 类中,我使用了两种转换方法,如下所示:
<Window ...
x:Name="Main"
xmlns:Base="clr-namespace:InsightTool.Gui.Helper">
<Window.Resources>
<Base:ThreadsAvailableCountToColorConverter x:Key="ThreadsAvailableCntConverter"/>
<Base:AverageExecutionTimeToColorConverter x:Key="AvgExecutionTimeConverter"/>
</Window.Resources>
<!-- This one works fine-->
<TextBlock Width="10" Height="10" VerticalAlignment="Center" Background="{Binding ExecutionTimeAverage, Converter={StaticResource AvgExecutionTimeConverter}, ElementName=UCExecutionTimes}"/>
<!-- This one does not work-->
<TextBlock Width="10" Height="10" VerticalAlignment="Center" Background="{Binding ThreadsAvailableCount, Converter={StaticResource ThreadsAvailableCntConverter}, ElementName=Main}"/>
</Window>
DependencyProperties的声明:
public partial class UserControlExecutionTimes : UserControl {
public static readonly DependencyProperty ExecutionTimeAverageProperty =
DependencyProperty.Register("ExecutionTimeAverage", typeof(double), typeof(MainWindow), new FrameworkPropertyMetadata(double));
public double ExecutionTimeAverage {
get { return (double)GetValue(ExecutionTimeAverageProperty); }
set { SetValue(ExecutionTimeAverageProperty, value); }
}
}
public partial class MainWindow : Window {
public static readonly DependencyProperty ThreadsAvailableCountProperty = DependencyProperty.Register("ThreadsAvailableCount", typeof(int),
typeof(MainWindow), new FrameworkPropertyMetadata(int));
public int ThreadsAvailableCount {
get { return (int)GetValue(ThreadsAvailableCountProperty); }
set { SetValue(ThreadsAvailableCountProperty, value); }
}
}
DependencyProperties 均已正确设置,并且它们的值显示在 GUI 中。我在这里想念什么?
编辑:
我还测试了以下内容:
<Window>
<!-- This one works fine-->
<TextBlock Width="10" Height="10" VerticalAlignment="Center" Background="{Binding ThreadsAvailableCount, Converter={StaticResource AvgExecutionTimeConverter}, ElementName=Main}"/>
<!-- This one does not work-->
<TextBlock Width="10" Height="10" VerticalAlignment="Center" Background="{Binding ThreadsAvailableCount, Converter={StaticResource ThreadsAvailableCntConverter}, ElementName=Main}"/>
</Window>
Binding 消耗“新”转换器的返回值似乎有问题,但我不知道为什么。
EDIT2
我用 Snoop 检查绑定,结果如下:
但是不工作的转换器绑定的background 属性看起来是这样的:
另一个证明ThreadsAvailableCount 设置正确(绑定到Textblock):
显示ThreadsAvailableCountToColorConverter的返回值似乎越来越错误。这是因为在调试模式下,它会在ThreadsAvailableCountToColorConverter 的Convert 方法中的断点处停止。甚至在Convert方法中成功到达return。
【问题讨论】:
-
你有没有试过在 ThreadsAvailableCountToColorConverter 的 Convert 方法中放一个断点,看它是否被调用。
-
@R.Rusev 它正在被调用。我已经试过了,但是这个控件的背景颜色只是保持白色
-
调试器的输出窗口是否有异常或绑定错误?
-
第二个 TextBlock 是否可能不在您预期的位置或不在顶部。假设您使用的是网格?
-
删除
ValueConversion attribute看看。
标签: c# wpf xaml binding valueconverter