2011-08-24 108 views
1

Silverlight中是否存在多状态切换按钮?我试图在一个组中重新尝试模拟单选按钮,但是我想制作一个跨切换状态的故事板,并且因为单选按钮是单独的对象,我被卡住了(我试图让它看起来像是iPhone的切换器按钮,除了多态)Silverlight多状态切换按钮

回答

0

标准的Silverlight CheckBox作为IsThreeState财产。第三种状态的默认视觉效果是“ - ”,因此您可能需要自定义视觉效果。

CheckBox是ToggleButton的一个特例。

0

你可能知道OOTB CheckBox控件:

http://samples.msdn.microsoft.com/Silverlight/SampleBrowser/#/?sref=System.Windows.Controls.CheckBoxEx

这可能是有用的,但我的项目之一,它必须是类似的功能,但使用的输入而是使用自定义的UserControl方法。我强烈推荐它。

在用户控件的构造函数中,InitializeComponent();之后;您可以指定影响UI的值。时间允许我会注释一些xaml + cs。

下面是一些代码:

XAML:

<UserControl x:Class="SilverlightApplication1.MainPage" 
    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" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <Grid.Resources> 
      <Style x:Key="TernaryTB" TargetType="TextBox"> 
       <Setter Property="HorizontalAlignment" Value="Left"/> 
       <Setter Property="VerticalAlignment" Value="Top"/> 
       <Setter Property="Width" Value="64"/> 
       <Setter Property="Height" Value="39"/> 
       <Setter Property="FontWeight" Value="ExtraBold"/> 
       <Setter Property="FontFamily" Value="Portable User Interface"/> 
       <Setter Property="FontSize" Value="18"/> 
       <Setter Property="TextAlignment" Value="Center" /> 
      </Style> 
     </Grid.Resources> 
     <TextBox x:Name="BackgroundTextBox" IsEnabled="True" BorderBrush="White" Foreground="{x:Null}" Visibility="Visible" Style="{StaticResource TernaryTB}"/> 
     <TextBox x:Name="TernaryTextBox" Style="{StaticResource TernaryTB}" GotFocus="TernaryTextBox_GotFocus"/> 
    </Grid> 

</UserControl> 

CS:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Collections.ObjectModel; 

namespace SilverlightApplication1 
{ 
    public partial class MainPage : UserControl 
    { 
     private int current; 
     public int Current 
     { 
      get 
      { 
       return current; 
      } 
      set 
      { 
       current = value; 
      } 
     } 

     public struct DataStructure 
     { 
      private string label; 
      private Color color; 
      private Color forecolor; 

      public DataStructure(string label, Color c, Color c2) 
      { 
       this.label = label; 
       this.color = c; 
       this.forecolor = c2; 
      } 

      public string Label 
      { 
       get { return label; } 
       set { label = value; } 
      } 
      public Color Color 
      { 
       get { return color; } 
       set { color = value; } 
      } 
      public Color Forecolor 
      { 
       get { return forecolor; } 
       set { forecolor = value; } 
      } 

     } 
     IList<DataStructure> DataStructureArray; 

     public MainPage() 
     { 
      InitializeComponent(); 
      current = 0; 
      int i = 0; 

      DataStructureArray = new Collection<DataStructure> 
      (new[] { 
       new DataStructure ("A", Colors.Red,Colors.Green), 
       new DataStructure ("B", Colors.Green,Colors.Blue), 
       new DataStructure ("C", Colors.Blue,Colors.Red) 
      }); 

      foreach (DataStructure listItem in DataStructureArray) 
      { 
       if (i == current) 
       { 
        TernaryTextBox.Text = listItem.Label; 
        TernaryTextBox.Background = new SolidColorBrush(listItem.Color); 
        break; 
       } 
       i++; 
      } 
     } 
     public MainPage(IList<DataStructure> NewDataStructureArray) 
     { 
      int i = 0; 
      DataStructureArray = new Collection<DataStructure>(); 
      foreach (DataStructure ds in NewDataStructureArray) 
      { 
       DataStructureArray.Add(new DataStructure(ds.Label, ds.Color, ds.Forecolor)); 
       i++; 
      } 
     } 

     private void TernaryTextBox_GotFocus(object sender, RoutedEventArgs e) 
     { 
      int i = 0; 
      current = (current + 1) % 3; 

      foreach (DataStructure listItem in DataStructureArray) 
      { 
       if (i == current) 
       { 
        TernaryTextBox.Text = listItem.Label; 
        TernaryTextBox.Background = new SolidColorBrush(listItem.Color); 
        TernaryTextBox.Foreground = new SolidColorBrush(listItem.Forecolor); 

        BackgroundTextBox.Focus(); 
        break; 
       } 
       i++; 
      } 
      BackgroundTextBox.Focus(); 
     } 
    } 
} 

要测试,只需一个品牌打屁股用户控件添加到项目中,复制粘贴我的代码;来测试我的孤立的代码,打开App.xaml.cs并只需更换:

this.RootVisual = new MainPage(); 

有:

this.RootVisual = new <Whatevernameyouchosefortheusercontrol>(); 

看到它在行动

+0

谢谢。我会考虑这个 – Aks