In my UWP app, I have a series of AppBarButtons
separated by AppBarSeparators
. When the window size drops below a certain amount, I want to hide the AppBarSeparators
to save space.
I tried something like this, but it didn't work:
<VisualState.Setters>
<Setter Target="AppBarSeparator" Value="Collapsed"/>
</VisualState.Setters>
I understand it's not possible to give each of the AppBarSeparators
labels so I can target them directly, because they are dynamically generated as part of a binding.
So how can I hide all AppBarSeparators when my window shrinks below a certain size?
Edit: Here is a stripped down version of my XAML to show how the AppBarSeparators are being generated:
<Pivot x:Name="docPivot"
ItemsSource="{Binding}">
<Pivot.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal"
Grid.Row="0">
<AppBarButton/>
<AppBarSeparator/>
<AppBarButton/>
<AppBarButton/>
<AppBarSeparator/>
</StackPanel>
<StackPanel Orientation="Horizontal"
Grid.Row="1">
</StackPanel>
</Grid>
</DataTemplate>
</Pivot.ItemTemplate>
</Pivot>
As we've discussed in the comment, your
AppBarSeparators
are generated in thePivot
'sDataTemplate
, when controls are placed inside theDateTemplate
, they becomes the visual structure of your data objects, butVisualState
targets the controls, so can it not work here.You can use DataBinding with Converter to do this, and if the size of the window is changeable during the run-time, you may also need complete your data source class with INotifyPropertyChanged Interface.
For example here:
Code behind, I used FrameworkElement.SizeChanged event to get the window's width during the run-time, if you just want to make the layout fit for mobile or PC at the first time the layout loading is, then this event is not needed, neither is the
INotifyPropertyChanged
:The
MyPivotItem
class is like this:And the
BoolVisibleConverter
is quite simple here:You can define a dependency property in your page:
Then bind Visibility property of the AppBarSeparators to this property:
Then change the SeparatorVisibility property of the page in the visual state:
The visual state changes the page property, it will change visibility of AppBarSeparators since their Visibility property are bound to the SeparatorVisibility property of the page. Not sure if it is the best solution, it's just what comes to my mind now.