Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight IValueConverter不适用于SolidColorBrush_Silverlight_Windows Phone 7_Data Binding_Ivalueconverter - Fatal编程技术网

Silverlight IValueConverter不适用于SolidColorBrush

Silverlight IValueConverter不适用于SolidColorBrush,silverlight,windows-phone-7,data-binding,ivalueconverter,Silverlight,Windows Phone 7,Data Binding,Ivalueconverter,我有一个进度条,我想根据布尔值改变颜色;真是绿,假是红。我的代码看起来应该可以工作(当我将它绑定到文本框时,它会返回正确的值),但当它是进度条的颜色属性时,它就不会工作了。转换器的定义如下(在App.xaml.cs中,因为我想在任何地方访问它): 然后,我将以下内容添加到App.xaml(这样它就可以成为一个全局资源): 这样就可以构建和测试它了。输出的图像如下所示: 它从转换器返回文本框的值,但不返回进度条。当我运行调试器时,它甚至不调用它 谢谢你的帮助 尝试修改转换器以返回一个SolidC

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

然后,我将以下内容添加到App.xaml(这样它就可以成为一个全局资源):

这样就可以构建和测试它了。输出的图像如下所示:

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


谢谢你的帮助

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

如果转换器返回一个SolidColorBrush,而您直接将其绑定到ProgressBar的前台属性,这难道不起作用吗?哇-真管用。我仍然掌握着xaml的窍门,所以我没有尝试过。如果你把这个作为回答,我会接受的。谢谢你的建议@泰勒·苏思威克将其作为答案发布。我很高兴能帮上忙。:)@TaylorSouthwick-侧注,但如果这是您正在使用的标准ProgressBar,我建议您避免使用它。使用Jeff Wilcox的
PerformanceProgressBar
,或使用
系统托盘中的
ProgressIndicator
。更多信息:@keyboard谢谢,我来看看
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;
    }
}
<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>
<Grid x:Name="LayoutRoot" Background="Transparent">
    <ListBox x:Name="listBox" 
             ItemTemplate="{StaticResource ItemTemplate}"/>
</Grid>
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;
        }
    }
}