使用IValueConverter启用/禁用按钮的IsDefault属性

时间:2013-03-09 15:12:58

标签: c# xaml mvvm ivalueconverter

我的视图有两个名为BrowseButton和UploadButton的按钮。

BrowseButton is default而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类的情况吗?

EG

public class BoolConverterForThis : IValueConverter
{//implementation}
public class BoolConverterForThat : IValueConverter
{//implementation}
public class BoolConverterForOther : IValueConverter
{//implementation}

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

我遇到了同样的问题,我不得不求助于编写True Visibility转换器和False Visiility转换器。 并不是说您无法对可见性转换器进行编码,但为了不这样做的人的利益,这就是我的代码的样子。

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);
        }
    }