简体   繁体   English

更改列表框项目的可见性

[英]Change ListBox item visibility

I have List box with multiple items in it like TextBlock,Image etc... 我有列表框,其中有多个项目,例如TextBlock,Image等...

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文件中,所有项的可见性都将被折叠,在我的.CS文件中,基于以下条件:我决定要显示的项就像我只需要显示TextBlock或Image一样,但是由于默认情况下所有项的可见性都处于折叠状态,因此动态更改ListBoxItems可见性并将数据或图像设置为项目?

Here is my XAML code: 这是我的XAML代码:

<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>

EDIT 编辑

I am paht in my .CS file to an image source like this : 我将自己的.CS文件打包到这样的图像源中:

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

Now in the the Image in XAML file how to bind this path to it ? 现在在XAML文件中的Image中如何将此路径绑定到它?

Usually we do it like this : 通常我们这样做:

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

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 ? 这里MyImage是XAML文件中Image的名称,但是在我的情况下,我无法获取列表框中的图像名称,因此现在如何绑定数据?

Just add a properties TextVisiblity and ImageVisibility to your view-model. 只需向视图模型添加属性TextVisiblityImageVisibility 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; }
}

Alternatively, if you don't want to modify the model class, you can use an IValueConverter , eg: 另外,如果您不想修改模型类,则可以使用IValueConverter ,例如:

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

(For this you'd have to write the ModelToTextVisibility class, see here for a full example of how to do it.) (为此,您必须编写ModelToTextVisibility类,有关如何执行此操作的完整示例,请参见此处 。)

this is how I get a listbox item in code. 这就是我在代码中获取列表框项目的方式。

 ListBoxItem lbi = listBox.ItemContainerGenerator.ContainerFromIndex(listBox.SelectedIndex) as ListBoxItem;

and thats how I make it invisible. 那就是我如何使其不可见。

 lbi.Visibility = Visibility.Collapsed;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM