0

私のビューには、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}
4

2 に答える 2

1

私はこのようなことをしたくなります:

public class StringIsEmptyToBoolConverter : MarkupExtension, IValueConverter
{
    public StringIsEmptyToBoolConverter()
    {
        this.Result = false;
    }

    public bool Result { get; set; }

    public object Convert(object value, Type targetType, 
                          object parameter, CultureInfo culture)
    {
        return string.IsNullOrEmpty(value as string) ? this.Result : !this.Result;
    }

    public object ConvertBack(object value, Type targetType, 
                              object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

Xamlで次のように使用できます。

<Window.Resources>
    <wpfApplication2:StringIsEmptyToBoolConverter 
        x:Key="TrueResultConverter" Result="True"/>
    <wpfApplication2:StringIsEmptyToBoolConverter 
        x:Key="FalseResultConverter" Result="False"/>
</Window.Resources>
<Grid>
    <!-- because the converter derives from MarkupExtension you can 
         create an instance directly instead of creating a resource -->
    <Button IsDefault="{Binding File,
          Converter={wpfApplication2:StringIsEmptyToBoolConverter Result=True}}"/>

    <!-- or use a resource -->
    <Button IsDefault="{Binding File, 
          Converter={StaticResource TrueResultConverter}}"/>
    <Button IsDefault="{Binding File, 
          Converter={StaticResource FalseResultConverter}}"/>
</Grid>
于 2013-03-09T15:47:10.033 に答える
0

同じ問題が発生し、TrueVisibilityコンバーターとFalseVisiilityコンバーターのコーディングに頼らざるを得ませんでした。可視性コンバーターをコーディングできないわけではありませんが、そうでない人のために、これが私のコードのように見えました。

public class TrueVisibilityConverter : IValueConverter
{
    public object Convert(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        bool visibility = (bool)value;
        return visibility ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        Visibility visibility = (Visibility)value;
        return (visibility == Visibility.Visible);
    }
}



public class FalseVisibilityConverter : IValueConverter
    {
        public object Convert(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            bool visibility = (bool)value;
            return visibility ? Visibility.Collapsed:Visibility.Visible ;
        }

        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            Visibility visibility = (Visibility)value;
            return (visibility == Visibility.Collapsed);
        }
    }
于 2013-03-09T15:28:47.263 に答える