更改列表框项目可见性 [英] Change ListBox item visibility

查看:26
本文介绍了更改列表框项目可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含多个项目的列表框,例如 TextBlock、Image 等...

I have List box with multiple items in it like TextBlock,Image etc...

现在在 XAML 文件中,所有项目的可见性都将被折叠,在我的 .CS 文件中,我根据条件决定显示哪个项目,就像我只需要显示 TextBlock 或图像一样,但是由于所有项目的可见性默认情况下都是折叠的,如何动态改变ListBoxItems的可见性,给items设置数据或图片?

Now in the XAML file all the items visibility will be collapsed, in my .CS file based on a condition i deciding which item to display like i need to display only TextBlock or Image, but since all items visibility is collapsed by default, how to dynamically change the ListBoxItems visibility and set the data or image to the items?

这是我的 XAML 代码:

Here is my XAML code:

<ListBox Name="listBox" 
         HorizontalContentAlignment="Stretch" 
         VerticalContentAlignment="Stretch" 
         SelectionChanged="TopicListboxSelectionChanged"
         ScrollViewer.VerticalScrollBarVisibility="Disabled">
         <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Name="CellBack" Margin="0,0,0,4" Orientation="Horizontal">
                            <Border Name="borderColor" Background="#FFF2F4F7">
                                <TextBlock Name="text"
                                       Width="456"
                                       Padding="10,20,10,20"
                                       Visibility="Collapsed"
                                       TextAlignment="Center"
                                       Text="{Binding Path=Value}"
                                       Style="{StaticResource TextStyle}"/>
                            </Border>
                               <Image Name="Image"
                                       Grid.Row="0"
                                       Visibility="Collapsed"
                                       Width="Auto"
                                       Height="Auto"
                                       HorizontalAlignment="Center"
                                       VerticalAlignment="Center"
                                       Margin="1,1,1,1"/>
                        </StackPanel>
                     </DataTemplate>
                </ListBox.ItemTemplate>

编辑

我在我的 .CS 文件中找到了这样的图像源:

I am paht in my .CS file to an image source like this :

image = "Path to my soucre" + imagename +"phone.png";

现在在 XAML 文件中的 Image 中如何将这个路径绑定到它?

Now in the the Image in XAML file how to bind this path to it ?

通常我们这样做:

BitmapImage bmp = new BitmapImage(new Uri(fileName, UriKind.Relative));
                MyImage.Source = bmp;

这里的 MyImage 是 XAML 文件中 Image 的名称,但在我的情况下,我无法获取列表框中的图像名称,那么现在如何绑定数据?

here MyImage is the name of Image in the XAML file ,but in my case i cant get the image name which is in the list box so now how to bind the data ?

推荐答案

只需将 TextVisiblityImageVisibility 属性添加到您的视图模型.然后直接绑定到它们:

Just add a properties TextVisiblity and ImageVisibility to your view-model. Then bind to them directly:

<DataTemplate>
    <StackPanel>
        <TextBlock Visibility="{Binding TextVisibility}" ... />
        <Image Visibility="{Binding ImageVisibility}" ... />
    </StackPanel>
</DataTemplate>

属性可以是只读的,例如:

The properties can be read-only, as in for example:

public Visibility TextVisibility
{
    get { return Value == null ? Visibility.Collapsed : Visibility.Visible; }
}


或者,如果你不想修改模型类,你可以使用一个IValueConverter,例如:

<TextBlock Visibility="{Binding Converter={StaticResource ModelToTextVisibility}" ... />

(为此,您必须编写 ModelToTextVisibility 类,请参阅 此处 了解如何操作的完整示例.)

(For this you'd have to write the ModelToTextVisibility class, see here for a full example of how to do it.)

这篇关于更改列表框项目可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆