Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin按钮可见绑定,ValueConverter不工作_C#_Xaml_Xamarin - Fatal编程技术网

C# Xamarin按钮可见绑定,ValueConverter不工作

C# Xamarin按钮可见绑定,ValueConverter不工作,c#,xaml,xamarin,C#,Xaml,Xamarin,我想根据ListView项的BindingContext的值显示/隐藏一个按钮,因此我制作了一个ValueConverter将BindingContext转换为布尔值。由于某些原因,它不起作用,即使值为null,按钮也始终可见 编辑:初始化转换器时返回正确的值,true/false,但似乎未设置IsVisible。 当更改绑定到Convert方法的项时,这对我来说也很奇怪,因为我希望它在绑定到的对象改变值时更新 这是转换器: public class NullToBoolConverter :

我想根据ListView项的BindingContext的值显示/隐藏一个按钮,因此我制作了一个ValueConverter将BindingContext转换为布尔值。由于某些原因,它不起作用,即使值为null,按钮也始终可见

编辑:初始化转换器时返回正确的值,true/false,但似乎未设置IsVisible。 当更改绑定到Convert方法的项时,这对我来说也很奇怪,因为我希望它在绑定到的对象改变值时更新

这是转换器:

public class NullToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value != null ? true : false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Not used.
        throw new NotImplementedException();
    }
}
下面是我在XAML中使用它的方式:

<converters:NullToBoolConverter x:Key="objectToBool" />
<DataTemplate x:Key="MyItemTemplate">
            <ViewCell>
                <Grid ColumnSpacing="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <controls:ImageButton Grid.Column="0"
                                          IsVisible="{Binding Path=., Converter={StaticResource objectToBool}}"
                                          VerticalOptions="Start" HorizontalOptions="Center"
                                          Image="ic_remove_circle_outline_black_24dp"
                                          BackgroundColor="Transparent" />

                </Grid>
            </ViewCell>
        </DataTemplate>

以及listview本身的价值

<ListView ItemsSource="{Binding MyItems}" 
                  ItemTemplate="{StaticResource MyItemTemplate}" 
                  RowHeight="50"
                  HeightRequest="155"
                  VerticalOptions="Start"
                  BackgroundColor="#209FAA9F"/>

根据您显示的代码,您的值转换器似乎不在ResourceDictionary中。将
NulltoBoolConverter
放入页面的资源字典中,尝试修改XAML:

<ContentPage.Resources>
    <ResourceDictionary>
        <converters:NullToBoolConverter x:Key="objectToBool" />
    </ResourceDictionary>
</ContentPage.Resources>


供将来参考:

根据您所显示的代码,您的值转换器似乎不在ResourceDictionary中。将
NulltoBoolConverter
放入页面的资源字典中,尝试修改XAML:

<ContentPage.Resources>
    <ResourceDictionary>
        <converters:NullToBoolConverter x:Key="objectToBool" />
    </ResourceDictionary>
</ContentPage.Resources>

供日后参考: