我使用用户控件创建了一个标签栏,因为我需要标签栏位于多个页面上,因为它会更改当前页面。这意味着标签栏必须通过在正确图像后面移动一个黑色矩形来显示当前显示的页面。所以我尝试使用一个名为 SelectionIndex 的自定义属性将选择矩形移动到正确图像后面,0 是主图像,1 是计划图像等。下面的代码显示了在应该选择图像的页面上使用的标签栏第二个。
<Grid>
<local:TabBar SelectionIndex="1" Margin="0,0,1222,0"/>
</Grid>
下面的代码是由生成的propdp
,我将属性的名称更改为 SelectionIndex。然后我使用 Switch-Case 检查属性的值是什么,并更改矩形图像的边距以移动它。
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;
}
}
}
但是,我设置了一个断点switch (SelectionIndex)
来检查 SelectionIndex 的值,它是 0 但应该是 1。为了自己解决这个问题,我将属性类型从 int 更改为字符串,这表明 SelectionIndex 是'不是“0”,它是空的。
我已经用谷歌搜索了这个问题,但似乎其他人都无法将值绑定到属性,我只想能够在 XAML 中将属性设置为 0-5 之间的 int。