【问题标题】:IValueConverter not working for SolidColorBrushIValueConverter 不适用于 SolidColorBrush
【发布时间】:2012-02-07 23:45:11
【问题描述】:

我有一个进度条,我想根据布尔值更改颜色; true 为绿色,false 为红色。我的代码看起来应该可以工作(当我将它绑定到文本框时它返回正确的值),但当它是进度条的颜色属性时却不行。转换器是这样定义的(在 App.xaml.cs 中,因为我想在任何地方访问它):

public class ProgressBarConverter : System.Windows.Data.IValueConverter
{
    public object Convert(
        object o, 
        Type type, 
        object parameter, 
        System.Globalization.CultureInfo culture)
    {
        if (o == null)
            return null;
        else
            //return (bool)o ? new SolidColorBrush(Colors.Red) : 
            //                 new SolidColorBrush(Colors.Green);
            return (bool)o ? Colors.Red : Colors.Green;
    }

    public object ConvertBack(
        object o, 
        Type type, 
        object parameter, 
        System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

然后我将以下内容添加到 App.xaml(因此它可以是全局资源):

<Application.Resources>
    <local:ProgressBarConverter x:Key="progressBarConverter" />
    <DataTemplate x:Key="ItemTemplate">
        <StackPanel>
            <TextBlock Text="{Binding name}" Width="280" />
            <TextBlock Text="{Binding isNeeded, 
                          Converter={StaticResource progressBarConverter}}" />
            <ProgressBar>
                <ProgressBar.Foreground>
                    <SolidColorBrush Color="{Binding isNeeded, 
                             Converter={StaticResource progressBarConverter}}" />
                </ProgressBar.Foreground>
                <ProgressBar.Background>
                    <SolidColorBrush Color="{StaticResource PhoneBorderColor}"/>
                </ProgressBar.Background>
            </ProgressBar>
        </StackPanel>
    </DataTemplate>
</Application.Resources>

我将以下内容添加到 MainPage.xaml 以显示它们:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <ListBox x:Name="listBox" 
             ItemTemplate="{StaticResource ItemTemplate}"/>
</Grid>

然后在 MainPage.xaml.cs 中,我定义了一个类来保存数据并将其绑定到 listBox:

namespace PhoneApp1
{
    public class TestClass
    {
        public bool isNeeded { get; set; }
        public string name { get; set; }
    }

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            var list = new LinkedList<TestClass>();
            list.AddFirst(
                                new TestClass { 
                                    isNeeded = true, name = "should be green" });
            list.AddFirst(
                                new TestClass { 
                                    isNeeded = false, name = "should be red" });
            listBox.ItemsSource = list;
        }
    }
}

我附上了一个minimal working example,所以它可以被构建和测试。输出的图像在这里:

它从转换器返回文本框的值,但不返回进度条。当我运行调试器时,它甚至没有调用它。

感谢您的帮助!

【问题讨论】:

  • 如果您的转换器返回一个solidColorBrush,并且您直接将其绑定到ProgressBar 的ForeGround 属性,它不会工作吗?
  • 哇 - 这工作。我仍然掌握 xaml 的窍门,所以这不是我尝试过的。如果你把它作为答案,我会接受它。感谢您的建议!
  • @TaylorSouthwick 将其作为答案发布。我很高兴能帮上忙。 :)
  • @TaylorSouthwick - 旁注,但如果这是您使用的标准 ProgressBar,我建议您避免使用它。使用 Jeff Wilcox 的 PerformanceProgressBar 或在 SystemTray 中使用 ProgressIndicator。更多信息:forums.create.msdn.com/forums/t/88459.aspx
  • @keyboardP 谢谢,我去看看

标签: silverlight windows-phone-7 data-binding ivalueconverter


【解决方案1】:

尝试修改您的转换器以返回SolidColorBrush,然后直接绑定到您的ProgressBars Foreground 属性。

【讨论】:

    猜你喜欢
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多