none
如何切换image显示? RRS feed

  • 问题

  • 请问:
    一个ListView绑定到数据库表,表中有一bool列:IsHidden
    想依据此值,决定是否显示image,但image的Visibility属性设置为bool类型是无效的,请问如何做这个绑定?
    如能做得到在xaml代码中依据此值切换显示两幅image显示则最好不过。

    2009年5月17日 4:07

答案

  • 可以使用Converter来做,请参看文章: http://msdn.microsoft.com/en-us/library/ms752091.aspx 以及示例代码: C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples\WPFSamples\ConnectedData\DataBindingLab C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples\WPFSamples\ConnectedData\BindConversionMarkup
    2009年5月17日 6:21
  • 您好,

     

    Killmyday 在上一个贴子中提供了你这个问题的方法,下面是我一个现成的小例子:

    XAML code:

     

    <Window x:Class="WpfValueConverterTest.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:local="clr-namespace:WpfValueConverterTest"

        Title="Window3" Height="300" Width="300"  Name="win3" >

        <Window.Resources>

            <local:People  x:Key="data" />

            <local:BoolToVisbilityConverter x:Key="converter" />

        </Window.Resources>

        <StackPanel>

            <ListView ItemsSource="{Binding Source={StaticResource data}}">

                <ListView.View>

                    <GridView>

                        <GridViewColumn  Header="Name">

                            <GridViewColumn.CellTemplate>

                                <DataTemplate>

                                    <TextBlock Text="{Binding Path=Name}"

                                               Visibility="{Binding Path=IsHidden,Converter={StaticResource converter}}" />

                                </DataTemplate>

                            </GridViewColumn.CellTemplate>

                        </GridViewColumn>

                        <GridViewColumn  DisplayMemberBinding="{Binding IsHidden}" Header="IsHidden" />

                    </GridView>

                </ListView.View>

            </ListView>

        </StackPanel>

    </Window>

     

    后台code:

    using System;

    using System.Windows;

    using System.Windows.Data;

    using System.Collections.ObjectModel;

    namespace WpfValueConverterTest

    {

        public partial class Window1 : Window

        {

            public Window1()

            {

                InitializeComponent();

            }

        }

        public class People : ObservableCollection<Person>

        {

            public People()

            {

                for (int i = 0; i < 10; i++)

                {

                    this.Add(new Person()

                    {

                        Name = "Name" + i.ToString(),

                        IsHidden = (i % 2 == 0) ? true : false

                    });

                }

            }

        }

        public class Person

        {

            public string Name { get; set; }

            public bool IsHidden { get; set; }

        }

        public class BoolToVisbilityConverter : IValueConverter

        {

            #region IValueConverter Members

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

            {

                bool flag = false;

                bool.TryParse(value.ToString(), out flag);

                if (flag == true)

                {

                    return Visibility.Visible;

                }

                else return Visibility.Hidden;

            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

            {

                throw new NotImplementedException();

            }

            #endregion

        }

    }

     

    如有不明白的地方请继续回复。

     

    谢谢。


    Jim Zhou -MSFT
    2009年5月18日 3:32
    版主

全部回复

  • 可以使用Converter来做,请参看文章: http://msdn.microsoft.com/en-us/library/ms752091.aspx 以及示例代码: C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples\WPFSamples\ConnectedData\DataBindingLab C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples\WPFSamples\ConnectedData\BindConversionMarkup
    2009年5月17日 6:21
  • 您好,

     

    Killmyday 在上一个贴子中提供了你这个问题的方法,下面是我一个现成的小例子:

    XAML code:

     

    <Window x:Class="WpfValueConverterTest.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:local="clr-namespace:WpfValueConverterTest"

        Title="Window3" Height="300" Width="300"  Name="win3" >

        <Window.Resources>

            <local:People  x:Key="data" />

            <local:BoolToVisbilityConverter x:Key="converter" />

        </Window.Resources>

        <StackPanel>

            <ListView ItemsSource="{Binding Source={StaticResource data}}">

                <ListView.View>

                    <GridView>

                        <GridViewColumn  Header="Name">

                            <GridViewColumn.CellTemplate>

                                <DataTemplate>

                                    <TextBlock Text="{Binding Path=Name}"

                                               Visibility="{Binding Path=IsHidden,Converter={StaticResource converter}}" />

                                </DataTemplate>

                            </GridViewColumn.CellTemplate>

                        </GridViewColumn>

                        <GridViewColumn  DisplayMemberBinding="{Binding IsHidden}" Header="IsHidden" />

                    </GridView>

                </ListView.View>

            </ListView>

        </StackPanel>

    </Window>

     

    后台code:

    using System;

    using System.Windows;

    using System.Windows.Data;

    using System.Collections.ObjectModel;

    namespace WpfValueConverterTest

    {

        public partial class Window1 : Window

        {

            public Window1()

            {

                InitializeComponent();

            }

        }

        public class People : ObservableCollection<Person>

        {

            public People()

            {

                for (int i = 0; i < 10; i++)

                {

                    this.Add(new Person()

                    {

                        Name = "Name" + i.ToString(),

                        IsHidden = (i % 2 == 0) ? true : false

                    });

                }

            }

        }

        public class Person

        {

            public string Name { get; set; }

            public bool IsHidden { get; set; }

        }

        public class BoolToVisbilityConverter : IValueConverter

        {

            #region IValueConverter Members

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

            {

                bool flag = false;

                bool.TryParse(value.ToString(), out flag);

                if (flag == true)

                {

                    return Visibility.Visible;

                }

                else return Visibility.Hidden;

            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

            {

                throw new NotImplementedException();

            }

            #endregion

        }

    }

     

    如有不明白的地方请继续回复。

     

    谢谢。


    Jim Zhou -MSFT
    2009年5月18日 3:32
    版主
  • 就 bool 到 Visibility 的转换而言, 根本不需要自己写一个 Converter 类.
    因为 System.Windows.Controls 下已经内置了这个类: BooleanToVisibilityConverter.

    一个例子:
      <Grid x:Name="layoutRoot">
       <Grid.Resources>
        <BooleanToVisibilityConverter x:Key="bToV"/>
       </Grid.Resources>
       <StackPanel>
        <Button Visibility="{Binding Path=IsChecked, ElementName=chk, Converter={StaticResource bToV}}">Afx</Button>
        <CheckBox Name="chk"/>
       </StackPanel>
      </Grid>


    在 System.Windows.Controls 下有几个内置的 Converter 类 (可以为一些常用操作带来方便, 而不必再写一个类):
    AlternationConverter                             IValueConverter
    BooleanToVisibilityConverter                  IValueConverter
    BorderGapMaskConverter                      IMultiValueConverter
    MenuScrollingVisibilityConverter             IMultiValueConverter

    Hello, everyone
    2009年5月18日 15:50