问题描述
我正在使用WPF Prism.Mvvm.我在侧面有一个导航栏,其中包含一个绑定到Itemssource的Listview.这些项目包含图像和内容.现在,我的目标是折叠项目的内容,以便导航控件仅显示图标.
I am using WPF Prism.Mvvm.I have a navigation bar on the side containing a Listview bound to an Itemssource. Those Items contain an image and the content. Now my goal is to collapse the content of the item so that my navigation control only shows the icons.
我已经定义了一个Visibility绑定,在我的Listview之外的某些图像上使用了此绑定,它可以很好地工作.但是,一旦我尝试对Listview的itemtemplate中的任何内容使用确切的绑定,就什么也没有发生.有趣的是,如果我将可见性"设置为自己折叠而没有绑定",它将再次起作用.
I have defined a Visibility binding, using this on some image outside of my Listview which works perfectly. But as soon as I try to use that exact binding on anything within the itemtemplate of the Listview, nothing happens.The funny thing is, if I set the Visibility to Collapsed myself without a binding, it works again.
这是我的视图
<UserControl.Resources>
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />
</ResourceDictionary>
[...]
<DockPanel >
<ContentControl DockPanel.Dock="Top">
<StackPanel>
<Button Margin="5,5,5,5" Style="{StaticResource MyButton}" Command="{Binding ExpandMenuCommand}" Content="Menu" HorizontalAlignment="Left" Width="43.017" />
<ListView BorderThickness="0" VerticalContentAlignment="Top" SelectionMode="Single" ItemsSource="{Binding MenuItems}" SelectedItem="{Binding SelectedItem}" Background="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<Grid VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Margin="0,10,0,10" Grid.Column="0" Source="{Binding ImageSource}" />
<TextBlock Visibility="{Binding Visibility, Converter={StaticResource booleanToVisibilityConverter}}" VerticalAlignment="Center" Margin="10,10,10,10" Grid.Column="1" Text="{Binding Content}" Background="Transparent" FontFamily="Arial" FontWeight="Bold" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</ContentControl>
<ContentControl DockPanel.Dock="Bottom">
<Grid>
<Image Visibility="{Binding Visibility, Converter={StaticResource booleanToVisibilityConverter}}" HorizontalAlignment="Center" Height="59" Margin="19,0,0,25" Width="182" Source="/ModuleA;component/Images/Logo.png" VerticalAlignment="Bottom" />
</Grid>
</ContentControl>
</DockPanel>
现在,如果我将Visibility="{Binding Visibility, Converter={StaticResource booleanToVisibilityConverter}}"
复制到该Itemtemplate上方的任何内容(例如Listview本身或Stackpanel),则其工作方式类似于对Listview下的Image进行操作.但Listview.ItemTemplate中的Textblock,Image或Grid本身不会响应我的绑定.将其设置为Visibility="Collapsed"
,但是正如我希望的那样起作用.
Now if I copy Visibility="{Binding Visibility, Converter={StaticResource booleanToVisibilityConverter}}"
to ANYTHING above that Itemtemplate, say the Listview itself or the Stackpanel, it works like it does for the Image under the Listview. but the Textblock, or Image, or Grid itself within the Listview.ItemTemplate won't respond to my Binding. Setting it to Visibility="Collapsed"
however works just as I intend it to work.
我的视图模型:
private static bool _expanded = true;
private bool _visibility = _expanded;
public bool Visibility
{
get { return _visibility; }
set { SetProperty(ref _visibility, value); }
}
private void ExpandMenu()
{
_expanded = !_expanded;
Visibility = _expanded;
}
推荐答案
ListView.ItemTemplate是MenuItem对象的模板.我想Visibility
不是MenuItem类的属性,它属于另一个视图模型.因此,您需要从其他DataContext绑定它:
ListView.ItemTemplate is a template for MenuItem objects. I suppose Visibility
isn't a property of MenuItem class, it belongs in another view model. So you need to bind it froma different DataContext:
<TextBlock Visibility="{Binding DataContext.Visibility,
Converter={StaticResource booleanToVisibilityConverter},
RelativeSource={RelativeSource AncestorType=ListView}}"
VerticalAlignment="Center"
Margin="10,10,10,10" Grid.Column="1"
Text="{Binding Content}"
Background="Transparent"
FontFamily="Arial"
FontWeight="Bold" />
这篇关于WPF绑定到Listview的ItemTemplate的可见性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!