首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WPF中的数据绑定按钮可见性不起作用

WPF中的数据绑定按钮可见性不起作用
EN

Stack Overflow用户
提问于 2018-04-01 08:41:38
回答 1查看 1.2K关注 0票数 0

许多代码传入,其中一些可能是无用的,因为我尝试了很多事情来让它工作。

我正在尝试使按钮导出安装程序不可见,除非所选安装程序在IsDownloaded上的值为TRUE

所以我将从我的WPF开始

代码语言:javascript
复制
        DataContext="{Binding Source={StaticResource TheViewModel}}"
    Title="MainWindow" Height="458" Width="755">
<Window.Resources>
    <ViewModel:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
    <ViewModel:InstallerColorConverter x:Key="InstallerColorConverter" />
</Window.Resources>

<ListBox Height="300" Grid.Column="0" Grid.Row="0" SelectionMode="Single" ItemsSource="{Binding SelectedProduct.Installers}" SelectedItem="{Binding SelectedInstaller}" >
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Ellipse HorizontalAlignment="Right" Fill="{Binding InstallerRelativeToCurrent, Converter={StaticResource InstallerColorConverter}}" Margin="5,5,5,5 " Height="20" Width="20" Stroke="Black"></Ellipse>
                                    <TextBlock Margin="5,0,5,0" Text="{Binding Name}" />
                                    <TextBlock  Text=" " />
                                    <TextBlock Text="{Binding VersionNumber}" />
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                    <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Stretch">
                        <Button Name="btnInstall"  Margin="5,5,5,5" Height="50" Width="75" MouseDoubleClick="btnDownloadInstaller_MouseDoubleClick" >Download</Button>
                        <Button Name="btnExportInstaller" Visibility="{Binding Path=SelectedInstaller.IsDownloaded, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}, Converter={StaticResource BoolToVisibilityConverter}}" Margin="5,5,5,5" Height="50" Width="90" MouseDoubleClick="btnExportInstaller_MouseDoubleClick" >Export Installer</Button>
                    </StackPanel>

其中要指出的主要内容是

代码语言:javascript
复制
             <Button Name="btnExportInstaller" Visibility="{Binding Path=SelectedInstaller.IsDownloaded, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}, Converter={StaticResource BoolToVisibilityConverter}}" Margin="5,5,5,5" Height="50" Width="90" MouseDoubleClick="btnExportInstaller_MouseDoubleClick" >Export Installer</Button>

代码语言:javascript
复制
        <ViewModel:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />

现在在我的班上,BoolToVisibilityConver

代码语言:javascript
复制
    public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
    object parameter, CultureInfo culture)
    {
        var booool = (bool)value;
        if (booool)
            return Visibility.Collapsed;
        else
            return Visibility.Visible;

    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        // Do the conversion from visibility to bool
        return Visibility.Collapsed;
    }
}

最后,在视图模型中,我有

代码语言:javascript
复制
        private Visibility _IsVisible;
    public Visibility IsVisible
    {
        get { return _IsVisible; }
        set { _IsVisible = value; OnPropertyChanged(nameof(_SelectedInstaller.IsDownloaded)); } 
    }

   private Products _SelectedProduct;
    public Products SelectedProduct
    {
        get { return _SelectedProduct; }
        set { _SelectedProduct = value; OnPropertyChanged(nameof(SelectedProduct)); }

  private Installers _SelectedInstaller;
        public Installers SelectedInstaller
        {
            get { return _SelectedInstaller; }
            set { _SelectedInstaller = value; OnPropertyChanged(nameof(SelectedInstaller)); }

它不能工作

我在控制台上看到这两个错误,但我不理解它们

代码语言:javascript
复制
System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'System.Boolean' and 'System.Windows.Visibility'. Consider using Converter property of Binding. BindingExpression:Path=SelectedProduct.IsInstalled; DataItem='ViewModel' (HashCode=26987408); target element is 'Grid' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='False' BindingExpression:Path=SelectedProduct.IsInstalled; DataItem='ViewModel' (HashCode=26987408); target element is 'Grid' (Name=''); target property is 'Visibility' (type 'Visibility')

如果有人能指出我在这里做错了什么,我会永远爱你!

EN

回答 1

Stack Overflow用户

发布于 2018-04-01 09:44:18

SOLVED..this是工作代码

代码语言:javascript
复制
    <Window.Resources>
    <ViewModel:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />

<Button Name="btnExportInstaller" Visibility="{Binding SelectedInstaller.IsDownloaded , Converter={StaticResource BoolToVisibilityConverter}}" Margin="5,5,5,5" Height="50" Width="90" MouseDoubleClick="btnExportInstaller_MouseDoubleClick">Export Installer</Button>

    public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
    object parameter, CultureInfo culture)
    {
        var booool = (bool)value;
        if (booool == false)
            return Visibility.Collapsed;
        else
            return Visibility.Visible;

    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        if (value is Visibility && (Visibility)value == Visibility.Visible)
        {
            return true;
        }
        return false;
    }
}

内部视图模型

代码语言:javascript
复制
        private Installers _SelectedInstaller;
    public Installers SelectedInstaller
    {
        get { return _SelectedInstaller; }
        set { _SelectedInstaller = value; OnPropertyChanged(nameof(SelectedInstaller)); }

        private Visibility _IsVisible;
    public Visibility IsVisible
    {
        get { return _IsVisible; }
        set { _IsVisible = value; OnPropertyChanged(nameof(SelectedInstaller.IsDownloaded)); } 
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49593769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档