How to change the width of a control dynamically in UWP? - xaml

Tabbar is a very common navigation control on iOS and Android. But UWP doesn't seem to have.
I've seen an example XamlPivot(SHORTCUT)①, Use Pivot to make TabBar, The effect is very good, and I tried to modify it, so that the TabBar in the bottom, content in the upper.
My project is MasterDetail, Master part is TabBar(i.e TabsStyle Pivot), Detail part just a blank.
Now I found a big problem, TabBar each item does not automatically divide the width, then I try to use data binding and value converters to dynamically provide the width, the binding source is the ActuallyWidth of the MasterGrid, but the ActuallyWidth does not change with the window size, and when the Window on WideState, the Mater part will become blank.
So, How to change the width of the TabBarItem dynamically?
Various window size effect chart(Remove"()"):
(https:)//i.stack.imgur.com/3GE5t.png
(https:)//i.stack.imgur.com/FyQuX.png
(https:)//i.stack.imgur.com/pChwz.png
(https:)//i.stack.imgur.com/cib1l.png
XAML:
<Pivot x:Name="pivot"
Style="{StaticResource TabsStylePivotStyle}">
<PivotItem>
<PivotItem.Header>
<local:TabHeader Width="{Binding ActualWidth, Converter={StaticResource AutoWidthConverter}, ElementName=pivot, Mode=OneWay}"
Glyph=""
Label="item 1" />
</PivotItem.Header>
<TextBlock Text="Content content content" />
</PivotItem>
<PivotItem>
<PivotItem.Header>
<local:TabHeader Width="{Binding ActualWidth, Converter={StaticResource AutoWidthConverter}, ElementName=pivot, Mode=OneWay}"
Glyph=""
Label="item 2" />
</PivotItem.Header>
<TextBlock Text="Content content content" />
</PivotItem>
<PivotItem>
<PivotItem.Header>
<local:TabHeader Width="{Binding ActualWidth, Converter={StaticResource AutoWidthConverter}, ElementName=pivot, Mode=OneWay}"
Glyph=""
Label="item 3" />
</PivotItem.Header>
<TextBlock Text="Content content content" />
</PivotItem>
</Pivot>
Converter:
public object Convert(object value, Type targetType, object parameter, string language)
{
return (double)value / 3;
}

For the record, ActualWidth is NOT a DependencyProperty in UWP XAML model - so can't participate in bindings properly (it doesn't notify of changes).
So if you wish to do a binding like you're doing, you're going to need to expose ActualWidth in a bindable way. One of the easier ways of doing so is to a create a Behaviour explicitly for that, that attaches to the SizeChange event of the target element (in your case the pivot), and returns it's ActualWidth / ActualHeight / RenderSize as DependencyProperties on the behaviour. Your TabItems would then look to the ActualWidth on the behaviour instead.
(It's not done by default presumably because UWP XAML doesn't have read-only dependency property support, and binding too it can easily lead to circular layout cycles when layout rounding is in play if you're not careful)

Related

How do I edit the header of an Expander control in WinUI 3?

When I edit properties of the Expander control, the effects seem to only apply to the content of the Expander, rather than the header.
E.g. if I set the BorderBrush as black and the Background as aquamarine, those properties only apply to the content:
<Expander Header="Expander" BorderBrush="Black" Background="Aquamarine">
<TextBlock Text="Here is some text"/>
</Expander>
See what the above code looks like
I know I can use this syntax:
<Expander>
<Expander.Header>
<!--XAML content here-->
</Expander.Header>
</Expander>
and put XAML content inside the header. So I tried putting a StackPanel in there, and editing the border and background on that—but it only applied to a small portion of the Expander header, and didn't cover the drop down caret, for example.
How do I change properties of the header?
You can edit the default style but this might be easier:
<Border
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="SkyBlue"
BorderBrush="HotPink"
BorderThickness="1"
CornerRadius="{StaticResource ControlCornerRadius}">
<Expander
Content="CONTENT"
Header="HEADER" />
</Border>
You just need to set the Border's properties.

How to resize NavigationView and SplitView in UWP

I want to enable user to resize the NavigationView in my UWP app. Couldn't find any resources how I could do that.
I see some of the apps have "Resizable splitView" but for SplitView also, I cannot see any such resize property exposed to set by default.
Pls help.
Thanks in Advance
There are no such property can resize SplitView and NavigationView, you need to custom layout to implement a similar effect. You could use Slider control and bind the OpenPaneLength Property of SplitView to Slider.Value to do this. Please refer to the following code.
<Grid>
<SplitView Name="CoreSplitView"
DisplayMode="Inline"
IsPaneOpen="True"
OpenPaneLength="{Binding Value, ElementName=MasterSlider, Mode=OneWay}">
<SplitView.Pane>
<Grid Name="PaneGrid" Background="Gray">
<Slider Name="MasterSlider"
MinWidth="480"
VerticalAlignment="Stretch"
Maximum="480"
Minimum="10"
Opacity="0"
Value="150"
/>
<StackPanel Name="PaneStackPanel"
Margin="0,0,10,0" Background="LightGray">
<TextBlock TextWrapping="Wrap" Text="Use a slider when you want your users to be able to set defined, contiguous values (such as volume or brightness) or a range of discrete values (such as screen resolution settings).A slider is a good choice when you know that users think of the value as a relative quantity" />
</StackPanel>
</Grid>
</SplitView.Pane>
<Grid Name="ContentGrid" Background="LightSteelBlue">
</Grid>
</SplitView>
</Grid>
The StackPanel that is directly under our slider, acts like a cover for our Slider. Notice the StackPanel has Margin="0,0,10,0", this translates to a 10px distance from the right wall, which allows the Slider area to be exposed as a gray strip that is used to drag the Pane, again the 10px is arbitrary but it has to match the Minimum of the Slider.

combobox windows phone 8.1 no dropdown

I add a ComboBox in XAML for Windows Phone 8.1 in a StackPanel. On Running the app in the Emulator no Dropdown functionality is shown. If I Limit the StackPanel to a Height e.g. "70", only the 2 first Items are shown. If I say Height = "Auto" then all Items are shown immediately.
How can I enable the Dropdown functionality?
Header:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Combobox ..
<StackPanel Grid.Row="4" Width="350" HorizontalAlignment="Left">
<TextBlock x:Name="PlayerListPanel" TextWrapping="Wrap" Text="Select a Player" VerticalAlignment="Center" Margin="2,0,0,0" HorizontalAlignment="Left"/>
<ComboBox Name="StartPlayerComboBox" BorderThickness="1" >
<ComboBoxItem Tag="PLAYER1">Player 1</ComboBoxItem>
<ComboBoxItem Tag="PLAYER2">Player 2</ComboBoxItem>
<ComboBoxItem Tag="PLAYER3">Player 3</ComboBoxItem>
<ComboBoxItem Tag="Dummy1">1</ComboBoxItem>
<ComboBoxItem Tag="Dummy2">2</ComboBoxItem>
<ComboBoxItem Tag="Dummy3">3</ComboBoxItem>
</ComboBox>
</StackPanel>
Try LISTPICKER control..Its functions similar as ComboBox with Auto adjusting height and width..
<toolkit:ListPicker>
<toolkit:ListPickerItem Content="A+" />
<toolkit:ListPickerItem Content="B+" />
<toolkit:ListPickerItem Content="O+" />
</toolkit:ListPicker>
to use this Include following namespace in Xaml Page..ADD windows phone toolkit(click it)
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;
assembly=Microsoft.Phone.Controls.Toolkit"
if further info requires... revert back...
You should provide enough height for the combobox to open properly. If you limit the height, the combobox cannot extend beyond that. So it will show only a few items. Height = "Auto" means it can take whatever height as required by it. Thus the combobox will take the height required for it to open properly.
Combobox is Windows Phone/ Windows RunTime Environment behaves differently from its Silverlight/WPF Counterparts. Here we don't have Popup to show the List of items to be selected. Hence the dropdown needs enough space to show the options.
Best thing is to leave the Height as Auto, or MinWidth for any Width requirements.
Even tho, this is an old question, in addition to what they have said, already; a way to have an item selected as the default, you need to "mark it" as follow:
<ComboBoxItem IsSelected="True">Item 1</ComboBoxItem>
Then, once you click on it, you will have all other combo box items displayed (if you define them):
<ComboBox x:Name="myComboBox" VerticalAlignment="Top" Width="350" FontFamily="open sans" FontSize="20" Height="Auto" Foreground="DarkGray" BorderBrush="Black">
<ComboBoxItem IsSelected="True">Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
<ComboBoxItem>Item 3</ComboBoxItem>
<ComboBoxItem>Item 4</ComboBoxItem>
</ComboBox>
Hope it helps someone. Regards

DataTemplate does not show result of bound children

On my MainWindow.xaml page, I have the following code which works (where MyWord is a string)
<ContentControl Content="{Binding MyWord}" />
I'm playing with DataTemplates and trying understand them. So, I want to reference a DataTemplate from within my ContentControl. The DataTemplate should contain a TextBlock which binds to my string. I updated my code
<ContentControl ContentTemplate="{StaticResource ViewsTemplate}" />
And in my ResourceDictionary I add
<DataTemplate x:Key="ViewsTemplate">
<TextBlock Text="{Binding MyWord}" />
</DataTemplate>
This produces no text on screen at all. I even tried
<ContentControl Content="{Binding MyWord}" ContentTemplate="{StaticResource ViewsTemplate}" />
and still no result on screen.
I can't work out why can any one give some advice please.
Thank you
The ContentControl still needs to have some content bound to it.
<ContentControl ContentTemplate="{StaticResource ViewsTemplate}" Content="{Binding MyWord}" />
Would work, but then you'd need to change your data template because it expects to be able to find MyWord which of course it won't be able to, so you'd want to use just {Binding} instead.
Alternatively, bind the ContentControl's Content to {Binding} - the current DataContext of its parent - and leave the template as it is.

Xaml, How to bind a list of buttons over an image in different coordination

For a windows 8 Apps, i need to put a list of buttons over an image. Each button has CoordinateX and CoordinateY properties. I use a gridView to bind to the list of buttons.
I need to have the result as below:
Here is my code:
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" >
<Canvas>
<Image Source="ms-appx:///Assets/red.png" Stretch="None" ></Image>
<GridView ItemsSource="{Binding Buttons}"
SelectionMode="None" IsSwipeEnabled="false" IsItemClickEnabled="True" ItemClick="Button" Padding="0">
<GridView.ItemTemplate>
<DataTemplate>
<Button Width="194" Height="51" Background="Gray" Canvas.Left="{Binding CoordinateX}" Canvas.Top="{Binding CoordinateY}"></Button>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Canvas>
</Grid>
But this is what i get after running the app:
Ites Likely that at runtime before the X and Y co-ordinates can be set for the items within the GridView, the GridView itself has its properties applied.
Since
1) the GridView is intended to be used for oganization control
and
2) you havent forced the Grid to fill the view space
what you're left with is a GridView that acts as it wishes and limits the ability of its items to conform to your style, should the style attempt to force the items collection to live outside the bounds of the control.
You could try to set the GridView's Horizontal Alignment property but even if it happens to work, its probably a better bet to use a less organized content control like the more basic ItemsPresenter.
Which ever control you end up using... make sure to check for /set the HorizontalContentAlignment & VerticalContentAlignment properties if they exist.