6

ブール値に応じて色を変更したいプログレスバーがあります。真は緑、偽は赤です。私はそれが(テキストボックスにバインドするときに正しい値を返す)動作するはずだが、プログレスバーのカラープロパティのときはそうでないようなコードを持っている。 (私はどこにでもアクセスしたいので、App.xaml.csに)コンバータは、このように定義されていますSolidColorBrushでIValueConverterが機能しない

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で、私はデータを保持ボックスおよびリストボックスにバインドするクラスを定義します。

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を添付して作成してテストすることができます。出力の映像はここにある:

enter image description here

それは、テキストボックスのためのコンバータではなく、プログレスバーの値を返します。デバッガを実行すると、デバッガは呼び出しもしません。

ありがとうございました!

+0

あなたのコンバーターがsolidColorBrushを返し、これをあなたのProgressBarのForeGroundプロパティに直接バインドするとうまくいかないでしょうか? – BigL

+0

うわー - うまくいった。私はまだxamlのハングを取得しているので、それは私が試みたものではありません。あなたが答えとしてそれを置くなら、私はそれを受け入れるでしょう。提案していただきありがとうございます! –

+0

@TaylorSouthwick答えとして投稿しました。私は助けることができてうれしいです。 :) – BigL

答えて

3

SolidColorBrushを返すようにコンバータを変更してから、ProgressBars Foregroundプロパティに直接バインドしてください。

関連する問題