【发布时间】:2014-02-14 06:24:11
【问题描述】:
我有一个列表框,其中包含多个项目,例如 TextBlock、Image 等...
现在在 XAML 文件中,所有项目的可见性都将被折叠,在我的 .CS 文件中,我根据一个条件决定要显示的项目,就像我只需要显示 TextBlock 或图像一样,但由于所有项目的可见性默认情况下都是折叠的,如何动态改变 ListBoxItems 的可见性并将数据或图像设置到项目?
这是我的 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>
编辑
我的 .CS 文件中的图像源如下所示:
image = "Path to my soucre" + imagename +"phone.png";
现在如何在 XAML 文件中的 Image 中绑定这个路径呢?
通常我们这样做:
BitmapImage bmp = new BitmapImage(new Uri(fileName, UriKind.Relative));
MyImage.Source = bmp;
这里 MyImage 是 XAML 文件中 Image 的名称,但在我的情况下,我无法获取列表框中的图像名称,所以现在如何绑定数据?
【问题讨论】:
-
你如何决定哪个控件可见,有
Data和DataContext这个吗? -
我有一个对象,我可以检查它是否包含图像或文本,基于我必须使文本块或图像显示或显示在屏幕上,但该怎么做呢?跨度>
-
您的
Object在哪里?在ListItem的DataContext中? -
这里的 DataContext 是什么?我不知道,我的 .CS 文件中有我的对象
标签: c# wpf xaml windows-phone-8 windows-phone