Silverlight多状态切换按钮

时间:2011-08-24 05:24:26

标签: silverlight

银光中是否存在多态togglebutton这样的事情?我已经尝试重新组合一个组中的单选按钮,但是我想制作一个跨越切换状态的故事板,因为单选按钮是单独的对象我被卡住了(我试图让它看起来像iphone切换按钮,多状态除外)

2 个答案:

答案 0 :(得分:0)

您可能知道OOTB复选框控件:

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

这可能很有用,但对于我的一个项目,它必须是类似的功能,但使用输入框代替,所以我使用自定义UserControl方法。我强烈推荐它。

在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>();

看到它的实际效果

答案 1 :(得分:0)

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

CheckBox是ToggleButton的专业化。