私はコンバーターに夢中です。必要に応じて、値の「終了値」を変更するために使用する必要があることはわかっていますが、自分の場合に正しく使用する方法がわかりません。
シンプルな MVVM (3 つのフィールドのみ) と、アイテムのリストを含むメイン ウィンドウがあります。最初の項目は関数に応じて計算され、YES または NOT を示すことができ、他の値は直接バインドされます。
これはうまく機能していますが、最初の計算フィールドにある YES または NOT の値に応じて、背景色と前景色を変更する必要があります。例えば:
YES (must be blue) - ITEM 1
NO (must be grey) - ITEM 2
YES (must be blue) - ITEM 3
私のデータベースの内部値は次のとおりですが(この場合、計算はモジュラスです):
2 - ITEM 1
3 - ITEM 2
4 - ITEM 3
私のListBoxコードはこれです:
<phone:PhoneApplicationPage
x:Class="Pasti.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="My App" Style="{StaticResource PhoneTextNormalStyle}" />
<TextBlock Text="My List" Style="{StaticResource PhoneTextTitle1Style}" />
</StackPanel>
<ListBox x:Name="lstPills" Grid.Row="1" ItemsSource="{Binding AllItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" Width="440">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Background="HERE MUST GO THE CONVERTER, I SUPOSE">
<TextBlock Text="{Binding IsPair, Mode=TwoWay}"/>
</Border>
<TextBlock
Text="{Binding Name}"
FontSize="{StaticResource PhoneFontSizeLarge}"
Grid.Column="1"
VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</phone:PhoneApplicationPage>
このページの CS コードは次のとおりです。
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Set the page DataContext property to the ViewModel.
this.DataContext = App.ViewModel;
}
}
計算フィールドについては、これをモデルに追加しました (_myNumber はチェックする必要がある値を保持します)。
// Define a custom field based on some database values
// Get is calculated, while set will force it to refresh by Notifying
public string IsPair
{
get
{
return _myNumber % 2 == 0 ? "YES" : "NO";
}
set
{
NotifyPropertyChanged("IsPair");
}
}
注: リストを強制的に更新する他の方法がわからないため、set プロパティを通知と TwoWay モードのみに設定し、IsPair = ""
再計算したいときに a を実行します。他に方法があれば、よろしくお願いします。
では、この情報を使用して、IsPair 値に基づいてBackground
、Border のプロパティを Blue または Grey に設定する Converter を作成するにはどうすればよいでしょうか? 多くの Converter の例を見ましたが、これを正確に行うポイントはまだありません。
MainPage クラスの下の MainPage.cs に次のようなものを配置する必要があると思います。
// Converter for the YES-NO column on the list
public class IsPairConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (MY_CALCULATED_VALUE == "YES")
return "Blue";
return "Grey";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
しかし、MY_CALCULATED_VALUE を取得する方法とBackground
、Border の値にコンバーターを設定する方法は?