将int属性绑定到bool UIElement

时间:2016-08-04 11:30:14

标签: c# wpf xaml

我在XAML中有以下绑定,其中IsEnabled的类型为bool,而Length是Array的属性,类型为int。

<Button
        Content="Press Me"
        IsEnabled="{Binding MyArray.Length, FallbackValue=False}">
</Button>

当数组长度为0时,它会执行我想要的操作,当数组包含它启用的元素时,它会禁用。

但是,我没想到它会起作用,因为你不能在C#中隐式地从int转换为bool。

这样安全吗?

3 个答案:

答案 0 :(得分:5)

这种行为似乎没有记录在任何地方,所以你不应该依赖它。

一个安全而简单的解决方案是带有DataTrigger的样式:

<Button Content="Press Me">
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding MyArray.Length}" Value="0">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

答案 1 :(得分:1)

  

这样安全吗?

简而言之,是的,它是安全的 - 因为你有一个FallbackValue,如果绑定失败了。

你可以使用一个好的ol&#39; DataTrigger
我在WPF中忘记了他们(我最近在WinRT上工作很多 - 而且他们并没有在那里工作)

这会将触发器绑定到Length的{​​{1}},然后当条件满足时 - 将按钮的MyArray属性设置为(在本例中){{ 1}}。

IsEnabled

绑定到ViewModel上的false

您还可以在ViewModel上使用<Button Content="Press Me"> <Button.Style> <Style TargetType="Button"> <Style.Triggers> <DataTrigger Binding="{Binding MyArray.Length}" Value="0"> <Setter Property="IsEnabled" Value="False"/> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> 属性,然后将bool基于此:

public bool

并在您的Xaml中:

IsEnabled

使用public bool IsEnabled { get { return MyArray.Length > 0; } }
使用WinRT的任何人的最佳选择

您还可以创建并使用<Button Content="Press Me" IsEnabled="{Binding IsEnabled}}"> </Button> ,根据您之后的条件,可以回复IValueConverter

有关如何将转换器实现到WPF的精彩教程,请参阅http://wpftutorial.net/ValueConverters.html

转换器实现Converter,看起来像这样:

boolean

请勿忘记在您的用户控件/视图中引用它 - 甚至忘记在IValueConverter中引用它,以便将其用作public class BoolToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { // Do the conversion from bool to visibility } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { // Do the conversion from visibility to bool } }

<强>实例吗

您可以根据自己的逻辑以任何方式解释自己的转换器。

在这种情况下,你可以创建一个转换器,并在Xaml中使用它,如下所示:

你可以创建一个App.xaml,并在你的Xaml中使用它,如下所示:

StaticResource

或者只是简单地创建一个ArrayNotNullOrEmptyToBooleanConverter,然后在你的Xaml中使用它,就像这样:

<Button
    Content="Press Me"
    IsEnabled="{Binding MyArray, Converter={Binding ArrayNotNullOrEmptyToBooleanConverter}}">
</Button>

希望这有帮助! :)

答案 2 :(得分:1)

我不相信它会起作用,但确实如此

零是假的,其他一切都是真的

所有可以想象的是它将它视为零,其中零是假而其他每个数字都是1且是真的 这就是MS SQL Server中的位工作方式。

我不知道在未来的.NET版本中你可以指望它。

我甚至在后备工作中工作 我在TextBox上测试过 它甚至没有抛出一个有约束力的错误 如果我给它一个像iii这样糟糕的名字就会抛出一个绑定错误 如果绑定到字符串,则会抛出绑定错误

Problem is, custom fields aren't working for the div class-above-footer.
<div class="blog-posts-bottom">
            <div class="container">
                <h2>Connect with C-Level Executives</h2>
                <div class="blog-posts-slider">

                   <?php $temp_query = $wp_query;
                    $category_name = get_field('business_solutions_category_name');     
                         query_posts(array(
                                'category_name' =>  $category_name,
                                'posts_per_page' => 5
                        )
                     ); 
                     while (have_posts()) : the_post(); 
                     $thumb_id = get_post_thumbnail_id();
                     $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
                     $thumb_url = $thumb_url_array[0];
                     ?>

                     <div class="slider-single-post">
                        <div class="blog-image" style="background-image : url('<?php the_post_thumbnail_url(); ?>');"></div>
                        <div class="slider-post-text">
                            <h3><?php the_title(); ?></h3>
                            <p><?php the_excerpt(); ?></p>
                            <a href="<?php the_permalink(); ?>">Read More</a>
                        </div>
                    </div>

                    <?php endwhile; 
                    $wp_query = $temp_query; ?>

                </div>
            </div>
        </div>

        <div class="above-footer-link">
            <div class="container">
                <h2><?php the_field('business_solutions_abovefooter_heading');?></h2>
                <a href="<?php the_field('business_solutions_abovefooter_button_link');?>"><?php the_field('business_solutions_abovefooter_button_text');?></a>
            </div>
        </div>

或者你可以这样做

<TextBox Grid.Row="0" Grid.Column="0" 
        IsEnabled="{Binding ii}" PresentationTraceSources.TraceLevel="High"
        HorizontalAlignment="Stretch" 
        VerticalAlignment="Stretch" 
        Text="TexBox" />

public int ii { get { return 0; } }  
相关问题