0

我的视图有 2 个按钮,称为 BrowseButton 和 UploadButton。

BrowseButton是默认值,UploadButton 不是。我的目标是在单击 BrowseButton 后更改默认值(即 BrowseButton 不是默认值,但 UploadButton 是)。

单击 BrowseButton 时,会设置我的 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 类,因此它们都是相等的(即,如果一个是假的,那么两个都是假的!)。再次,我可以理解为什么。

我的问题是,我该如何解决这个问题?真的只是拥有多个 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

I had the same problem and I had to resort to coding a True Visibility converter and False Visiility converter. Not that you can't code a visibility converter, but for the benefit of someone who doesn't, this is what my code looked like.

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 回答