如何在XAML中创建动态接口

时间:2014-03-06 11:26:52

标签: wpf silverlight xaml dynamic interface

如果有人知道使用相关数据(DataContext =“{Binding User}”)或静态数据创建动态xaml的解决方案(DataContext =“{Binding User,Source = {StaticResource Locator}}”)?

这样的解决方案可以保存在隐藏控件和其他字段上(Visibility =“{Binding IsAnonim,Converter = {StaticResource VisibilityConverter}}”)。并且XAML只需要在这种情况下需要的东西,并且没有被不必要的控制所困扰。

代码(“if(IsUser){}”)可以在渲染时执行。

以下示例:

    <UserControl
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        d:DesignHeight="300" d:DesignWidth="400">
        <Grid x:Name="LayoutRoot" DataContext="{Binding User}">
            <StackPanel>
<? if (IsUser) { ?>
   <TextBox Text="{Binding UserName, Mode=TwoWay}" />
   <PasswordBox Text="{Binding UserPass, Mode=TwoWay}" />
<? } else if (IsAnonim) { ?>
   <TextBox Text="{Binding AnonimName, Mode=TwoWay}" />
<? } ?>
                <TextBox Text="{Binding MsgText, Mode=TwoWay}" />
                <Button Content="Button" Command="{Binding PostCommand}" />        
            </StackPanel>
        </Grid> </UserControl>

P.S。其他解决方案也是可能谢谢。

1 个答案:

答案 0 :(得分:0)

简单,具有绑定性和可见性

<UserControl
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   d:DesignHeight="300" d:DesignWidth="400">
   <UserControl.Resources>
     <Convertors:BooleanToInvisibiltyConverterx:Key="booleanToInvisibiltyConverter"/>
     <BooleanToVisibiltyConverterx:Key="booleanToVisibiltyConverter"/>
   </UserControl.Resources>

   <Grid x:Name="LayoutRoot" DataContext="{Binding User}">
     <StackPanel>
       <StackPanel Visibility="{Binding IsUser, Converter={StaticResource booleanToVisibiltyConverter}}">
         <TextBox Text="{Binding UserName, Mode=TwoWay}" />
         <PasswordBox Text="{Binding UserPass, Mode=TwoWay}" />
      </StackPanel>
      <StackPanel Visibility="{Binding IsUser, Converter={StaticResource booleanToInvisibiltyConverter}}">
        <TextBox Text="{Binding AnonimName, Mode=TwoWay}" />
      </StackPanel
      <TextBox Text="{Binding MsgText, Mode=TwoWay}" />
      <Button Content="Button" Command="{Binding PostCommand}" />        
    </StackPanel>
  </Grid>
</UserControl>

您只需要定义资源并创建一个InvisibiltyConverter

/// <summary>The boolean to invisibility converter.</summary>
public class BooleanToInvisibilityConverter : IValueConverter
{
    /// <summary>The convert.</summary>
    /// <param name="value">The value.</param>
    /// <param name="targetType">The target type.</param>
    /// <param name="parameter">The parameter.</param>
    /// <param name="culture">The culture.</param>
    /// <returns>The <see cref="object"/>.</returns>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null 
            && (bool)value == true)
        {
            return Visibility.Collapsed;
        }

        return Visibility.Visible;
    }

    /// <summary>The convert back.</summary>
    /// <param name="value">The value.</param>
    /// <param name="targetType">The target type.</param>
    /// <param name="parameter">The parameter.</param>
    /// <param name="culture">The culture.</param>
    /// <returns>The <see cref="object"/>.</returns>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然而,随着复杂性的增加,您会发现将所有UI保存在单个xaml文件中变得无法实现。此时,您需要了解@Clemens在评论中建议的数据模板。见Data templating overview