问题
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.
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.
here is my View
<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>
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.
My Viewmodel:
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;
}
回答1:
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" />
来源:https://stackoverflow.com/questions/51336633/wpf-binding-to-visibility-of-an-itemtemplate-of-a-listview-not-working