根据StackPanel中的属性显示控件

时间:2013-02-27 09:33:07

标签: c# windows-phone-8

我有一个StackPanel在Windows Phone应用中有三个TextBlock,在一个看起来像这样的页面上:

<StackPanel>
   <TextBlock></TextBlock>
   <TextBlock></TextBlock>
   <TextBlock></TextBlock>
</StackPanel>

根据属性值,我想显示TextBlock中的任何一个。假设我有一个名为“Name”的属性,并且如果此属性的值为“1”,我想仅显示第一个TextBlock,类似于2和3.有谁知道我怎么能做到这一点?

1 个答案:

答案 0 :(得分:0)

将可见性绑定到模型上的属性并使用转换器: http://windowsphonegeek.com/articles/talking-about-converters-in-wp7--coding4fun-toolkit-converters-in-depth

public class IntToVisibilityConverter : IValueConverter
    {

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
   int intValue (int)value;

    if(intValue == 1)
return Visibility.Visible;
else
return Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

}

相关问题