私のビューには、BrowseButtonとUploadButtonという2つのボタンがあります。
BrowserButtonはデフォルトですが、 UploadButtonはデフォルトではありません。私の目標は、BrowseButtonがクリックされた後にデフォルトを変更することです(つまり、BrowseButtonはデフォルトではありませんが、UploadButtonはデフォルトです)。
ブラウズボタンをクリックすると、ViewModelのプロパティ(文字列)が設定されます。これは、そのプロパティにバインドし、そのプロパティの値をIValueConverterに渡して、trueまたはfalseのいずれかを返すことができることを意味します。これは希望どおりに機能します。
私のBoolConverterは次のようになります
public class BoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || string.IsNullOrEmpty(System.Convert.ToString(value)))
return false;
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
そして私のXaml
<Button Content="Browse for file" Width="150" Margin="5" Command="{Binding BrowseForFileCommand}" IsDefault="{Binding File, Converter={StaticResource BConverter}}" />
<Button Content="Upload" Width="75" Margin="5" Command="{Binding UploadCommand}" IsDefault="{Binding File, Converter={StaticResource BConverter}}" />
問題は、両方が同じBoolConverterクラスにバインドされているため、両方が等しいことです(つまり、一方がfalseの場合、両方がfalseです!)。繰り返しますが、その理由は理解できます。
私の質問は、どうすればこれを回避できますか?それは本当に複数のConverterクラスを持っている場合ですか?
例えば
public class BoolConverterForThis : IValueConverter
{//implementation}
public class BoolConverterForThat : IValueConverter
{//implementation}
public class BoolConverterForOther : IValueConverter
{//implementation}