C# 如何在StackPanel中切换项目的可见性
我有一个列表框,其中包含两个项目的StackPanel,一个图像和一个文本块。根据用户的请求,我希望能够打开或关闭TextBlock的可见性,从而只显示图像。现在,每个项目的图像和文本块组合是垂直堆叠的,图像是一个完美的正方形(当文本块显示在每个图像下时,最终会创建一个矩形)。当用户希望隐藏TextBlock时,我希望ListBox仅将StackPanel项显示为图像的统一正方形(希望这是有意义的) 我所拥有的如下C# 如何在StackPanel中切换项目的可见性,c#,xaml,windows-phone-7,windows-phone-8,listbox,C#,Xaml,Windows Phone 7,Windows Phone 8,Listbox,我有一个列表框,其中包含两个项目的StackPanel,一个图像和一个文本块。根据用户的请求,我希望能够打开或关闭TextBlock的可见性,从而只显示图像。现在,每个项目的图像和文本块组合是垂直堆叠的,图像是一个完美的正方形(当文本块显示在每个图像下时,最终会创建一个矩形)。当用户希望隐藏TextBlock时,我希望ListBox仅将StackPanel项显示为图像的统一正方形(希望这是有意义的) 我所拥有的如下 <ListBox Name="ListBoxEffects" Select
<ListBox Name="ListBoxEffects" SelectionMode="Single" ItemsSource="{Binding}" Margin="{Binding}"
toolkit:TiltEffect.IsTiltEnabled="True" SelectionChanged="ListBox_SelectionChanged"
ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel ItemWidth="159" ItemHeight="Auto" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<Image Source="{Binding Thumbnail}" Width="155" Height="155" />
<TextBlock Text="{Binding Name}" Visibility="{Binding TextBlockVisibility}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeNormal}" VerticalAlignment="Center" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
当页面被导航到时,会执行一个检查,以便可以适当地显示或隐藏每个图像下的文本块
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (Settings.ShowFilterNames.Value)
//Show the TextBlocks here
else
//Hide the TextBlocks here
}
就我所知,上述实现确实正确地切换菜单项文本并保存用户的首选项,以便在返回菜单项文本时根据用户选择的最后一个选择显示,但我不确定如何更改列表框中每个图像下文本块的可见性
编辑**
BooleanToVisibilityConverter.cs
//Error on BooleanToVisibilityConverter stating does not implement interface member 'System.Windows.Data.IValueConverter.Convert(object, System.Type, object, System.Globalization.CultureInfo)
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo language)
{
return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo language)
{
return value is Visibility && (Visibility)value == Visibility.Visible;
}
}
在XAML中
xmlns:common="clr-namespace:TestApp.Common"
<phone:PhoneApplicationPage.Resources>
<common:BooleanToVisibilityConverter x:Key="BoolToVisConv" />
</phone:PhoneApplicationPage.Resources>
<ListBox Name="ListBoxEffects" SelectionMode="Single" ItemsSource="{Binding}" Margin="{Binding}"
toolkit:TiltEffect.IsTiltEnabled="True" SelectionChanged="ListBox_SelectionChanged"
ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel ItemWidth="159" ItemHeight="Auto" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<Image Source="{Binding Thumbnail}" Width="155" Height="155" />
<TextBlock Text="{Binding Name}" Visibility="{Binding IsTextBlockVisible, Converter={StaticResource BoolToVisConv}}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeNormal}" VerticalAlignment="Center" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
xmlns:common=“clr命名空间:TestApp.common”
可见性属性是类型为可见性的枚举。如果它是一个布尔值,这会容易一些,但事实并非如此
您应该定义一个静态资源来实例化BooleanToVisibility
转换器,然后将Visibility
属性绑定到DataContext中的布尔属性。下面是一个工作示例:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisConv" />
</Window.Resources>
<StackPanel>
<TextBlock Text="Hide me" Visibility="{Binding IsTextBlockVisible, Converter={StaticResource BoolToVisConv}}" />
<Button Content="Toggle TextBlock" Name="ToggleItButton" Click="ToggleItButton_Click" />
</StackPanel>
Visibility
属性是类型为Visibility
的枚举。如果它是一个布尔值,这会容易一些,但事实并非如此
您应该定义一个静态资源来实例化BooleanToVisibility
转换器,然后将Visibility
属性绑定到DataContext中的布尔属性。下面是一个工作示例:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisConv" />
</Window.Resources>
<StackPanel>
<TextBlock Text="Hide me" Visibility="{Binding IsTextBlockVisible, Converter={StaticResource BoolToVisConv}}" />
<Button Content="Toggle TextBlock" Name="ToggleItButton" Click="ToggleItButton_Click" />
</StackPanel>
Visibility
属性是类型为Visibility
的枚举。如果它是一个布尔值,这会容易一些,但事实并非如此
您应该定义一个静态资源来实例化BooleanToVisibility
转换器,然后将Visibility
属性绑定到DataContext中的布尔属性。下面是一个工作示例:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisConv" />
</Window.Resources>
<StackPanel>
<TextBlock Text="Hide me" Visibility="{Binding IsTextBlockVisible, Converter={StaticResource BoolToVisConv}}" />
<Button Content="Toggle TextBlock" Name="ToggleItButton" Click="ToggleItButton_Click" />
</StackPanel>
Visibility
属性是类型为Visibility
的枚举。如果它是一个布尔值,这会容易一些,但事实并非如此
您应该定义一个静态资源来实例化BooleanToVisibility
转换器,然后将Visibility
属性绑定到DataContext中的布尔属性。下面是一个工作示例:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisConv" />
</Window.Resources>
<StackPanel>
<TextBlock Text="Hide me" Visibility="{Binding IsTextBlockVisible, Converter={StaticResource BoolToVisConv}}" />
<Button Content="Toggle TextBlock" Name="ToggleItButton" Click="ToggleItButton_Click" />
</StackPanel>
您可以创建两个DataTemplate
。一个DataTemplate
有名称,另一个没有名称
XAML:
您可以创建两个DataTemplate
。一个DataTemplate
有名称,另一个没有名称
XAML:
您可以创建两个DataTemplate
。一个DataTemplate
有名称,另一个没有名称
XAML:
您可以创建两个DataTemplate
。一个DataTemplate
有名称,另一个没有名称
XAML:
使用此方法查找每个ListBoxItem的Textblock
public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
try
{
int childCount = VisualTreeHelper.GetChildrenCount(parentElement);
if (childCount == 0)
return null;
for (int i = 0; i < childCount; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
或
使用此方法查找每个ListBoxItem的Textblock
public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
try
{
int childCount = VisualTreeHelper.GetChildrenCount(parentElement);
if (childCount == 0)
return null;
for (int i = 0; i < childCount; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
或
使用此方法查找每个ListBoxItem的Textblock
public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
try
{
int childCount = VisualTreeHelper.GetChildrenCount(parentElement);
if (childCount == 0)
return null;
for (int i = 0; i < childCount; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
或
使用此方法查找每个ListBoxItem的Textblock
public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
try
{
int childCount = VisualTreeHelper.GetChildrenCount(parentElement);
if (childCount == 0)
return null;
for (int i = 0; i < childCount; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
或
马修,除了你的问题:
public class BooleanToVisibilityConverter : IValueConverter
{
private object GetVisibility(object value)
{
if (!(value is bool))
return Visibility.Collapsed;
bool objValue = (bool)value;
if (objValue)
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object Convert(object value, Type targetType, object parameter, string language)
{
return GetVisibility(value);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
马修,除了你的问题:
public class BooleanToVisibilityConverter : IValueConverter
{
private object GetVisibility(object value)
{
if (!(value is bool))
return Visibility.Collapsed;
bool objValue = (bool)value;
if (objValue)
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object Convert(object value, Type targetType, object parameter, string language)
{
return GetVisibility(value);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
马修,除了你的问题:
public class BooleanToVisibilityConverter : IValueConverter
{
private object GetVisibility(object value)
{
if (!(value is bool))
return Visibility.Collapsed;
bool objValue = (bool)value;
if (objValue)
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object Convert(object value, Type targetType, object parameter, string language)
{
return GetVisibility(value);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
马修,除了你的问题:
public class BooleanToVisibilityConverter : IValueConverter
{
private object GetVisibility(object value)
{
if (!(value is bool))
return Visibility.Collapsed;
bool objValue = (bool)value;
if (objValue)
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object Convert(object value, Type targetType, object parameter, string language)
{
return GetVisibility(value);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
我在实现您的解决方案时遇到问题,我正在为Windows Phone编程,因此它非常类似。您是否为在xaml中引用的转换器创建了一个类?我在
上遇到一个错误。我试图创建一个BooleantVisibilityConverter
类,但我也遇到了问题。我在上面添加了一些编辑。请看,我在实现您的解决方案时遇到问题,我正在为Windows Phone编程,因此它非常类似。您是否为在xaml中引用的转换器创建了一个类?我在
上遇到一个错误。我试图创建一个BooleantVisibilityConverter
类,但我也遇到了问题。我在上面添加了一些编辑。请看,我在实现您的解决方案时遇到问题,我正在为Windows Phone编程,因此它非常类似。您是否为在xaml中引用的转换器创建了一个类?我在
上遇到一个错误。我试图创建一个BooleantVisibilityConverter
类,但我也遇到了问题。我在上面添加了一些编辑。请看,我在实现您的解决方案时遇到问题,我正在为Windows Phone编程,因此它非常类似。您是否为在xaml中引用的转换器创建了一个类?我在
上遇到一个错误。我试图创建一个BooleantVisibilityConverter
类,但我也遇到了问题。我在上面添加了一些编辑。请参见