Silverlight內CheckBox控件與RadioButton控件的賦值與取值

1、方法一

<UserControl x:Class="SilverlightApplication1.MyListBox"
    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">
        <ListBox Width="300" Height="200">
            <StackPanel x:Name="sp">
               
            </StackPanel>
        </ListBox>
        <Button Width="120" Height="30" Margin="50,12,230,258" Content="GetSelectedValue" Click="Button_Click"></Button>
        <TextBlock x:Name="aaa" Text="123" Width="100" Height="30" Margin="203,12,97,258"></TextBlock>
    </Grid>
</UserControl>

CheckBox的獲取

public partial class MyListBox : UserControl
    {
        public MyListBox()
        {
            InitializeComponent();
            for (int i = 0; i < 10; i++)
            {
                CheckBox cb = new CheckBox();
                cb.Content = "cb" + i.ToString();
                sp.Children.Add(cb);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            aaa.Text = "";
            foreach (CheckBox c in sp.Children)
            {
                if (c.IsChecked == true)
                {
                    aaa.Text += c.Content.ToString() + ",";
                }
            }
        }
    }

RadioButton的獲取

public partial class MyListBox : UserControl
    {
        public MyListBox()
        {
            InitializeComponent();
            for (int i = 0; i < 10; i++)
            {
                RadioButton rb = new RadioButton();
                rb.Content = "rbtn" + i.ToString();
                sp.Children.Add(rb);
                          }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

aaa.Text = "";

            foreach (RadioButton rb in sp.Children)
            {
               if (rb.IsChecked == true)
              {
                   aaa.Text = rb.Content.ToString();
              }

}

方法二:

<Grid x:Name="LayoutRoot" Background="White">
        <ListBox x:Name="MyFeatures" Width="300" Margin="41,30,59,151">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <RadioButton GroupName="rbtn" Content="{Binding}" Checked="RadioButton_Checked"></RadioButton>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ListBox x:Name="MyCheckBox" Width="300" Margin="41,155,59,32">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Content="{Binding}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"></CheckBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBlock x:Name="aaa" Text="123" Height="30" Margin="80,12,30,258"></TextBlock>
        <Button Width="30" Height="30" Margin="358,258,12,12" Content="aaa" Click="Button_Click"></Button>
    </Grid>
            }
                               }
    }

 

List<string> list = new List<string>();
       
        public SilverlightListBox()
        {
            InitializeComponent();
            for (int i = 0; i < 6; i++)
            {
                list.Add("zhangsan"+i.ToString());
            }
            MyFeatures.ItemsSource = list;
            MyCheckBox.ItemsSource = list;
        }
        List<string> listcheck = new List<string>();
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            aaa.Text = (sender as RadioButton).Content.ToString();
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            listcheck.Add((sender as CheckBox).Content.ToString());
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            listcheck.Remove((sender as CheckBox).Content.ToString());
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            aaa.Text = "";
            for (int i = 0; i < listcheck.Count; i++)
            {
                aaa.Text += listcheck[i].ToString() + ",";
            }
        }

 

方法三:

<UserControl x:Class="SilverlightApplication1.ListBoxGrid"
    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">
        <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto"
                      Height="150" MinHeight="150" MaxHeight="150" Width="310">
            <ScrollViewer.Content>
                <Grid x:Name="Layout" Width="Auto" MinHeight="30" Height="Auto" Margin="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="280"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                </Grid>
            </ScrollViewer.Content>
        </ScrollViewer>
        <Button x:Name="MyButton" Width="60" Height="30" Content="GetValue" Click="Button_Click" Margin="314,250,26,20"></Button>
        <TextBlock x:Name="aaa" Text="123" Height="30" Margin="110,12,0,258"></TextBlock>
    </Grid>
</UserControl>

 

 public ListBoxGrid()
        {
            InitializeComponent();
            for (int i = 0; i < 10; i++)
            {
                RowDefinition row = new RowDefinition();
                row.Height = new GridLength(20);
                Layout.RowDefinitions.Add(row);

                CheckBox cb = new CheckBox();
                cb.Content = "checkb" + i.ToString();
                cb.SetValue(Grid.RowProperty, i);//設置這個纔可以正確排列
                Layout.Children.Add(cb);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            aaa.Text = "";
            foreach (CheckBox c in Layout.Children)
            {
                if (c.IsChecked == true)
                {
                    aaa.Text += c.Content.ToString() + ",";
                }
            }
        }

 

 

public ListBoxGrid()
        {
            InitializeComponent();
            for (int i = 0; i < 10; i++)
            {
                RowDefinition row = new RowDefinition();
                row.Height = new GridLength(20);
                Layout.RowDefinitions.Add(row);

                RadioButton rb = new RadioButton();
                rb.Content = "rbtn" + i.ToString();
                rb.SetValue(Grid.RowProperty, i);
                Layout.Children.Add(rb);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            aaa.Text = "";
            foreach (RadioButton c in Layout.Children)
            {
                if (c.IsChecked == true)
                {
                    aaa.Text += c.Content.ToString() + ",";
                }
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章