I've created a tab bar using a user control because I need the tab bar to be on multiple pages as it changes the current page. This means the tab bar has to show which page is currently shown by moving a dark rectangle behind the correct image. So I tried using a custom property called SelectionIndex to move the selection rectangle behind the correct image, 0 being the Home image and 1 being the Schedule image etc. The code below shows the tab bar being used on a page where the selected image should be the second one.
<Grid>
<local:TabBar SelectionIndex="1" Margin="0,0,1222,0"/>
</Grid>
The code below has been generated by propdp
and I changed the name of the property to SelectionIndex. I then use a Switch-Case to check what the value of the property is and change the margin of the rectangle image to move it.
public partial class TabBar : UserControl
{
public int SelectionIndex
{
get { return (int)GetValue(SelectionIndexProperty); }
set { SetValue(SelectionIndexProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectionIndex. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectionIndexProperty =
DependencyProperty.Register("SelectionIndex", typeof(int), typeof(TabBar), new PropertyMetadata(0));
public TabBar()
{
InitializeComponent();
switch (SelectionIndex)
{
case 0:
SelectionBox.Margin = new Thickness(0, 10, 0, 0);
break;
case 1:
SelectionBox.Margin = new Thickness(0, 123, 0, 0);
break;
}
}
}
However, I put a break point on switch (SelectionIndex)
to check the value of SelectionIndex, it was 0 but should be 1.
In an attempt to fix this myself, I changed the property type to a string from an int, this showed me that the SelectionIndex isn't "0", it's null.
I have Googled this issue but it seems everyone else is having trouble Binding a value to the property, I just want to be able to set the property to an int between 0-5 in the XAML.