如何在itemscontrol命名后使用itemscontrol动态创建控件的状态wpf C#

时间:2018-05-05 19:13:48

标签: c# wpf windows

对于我的代码,我使用绑定到类" MyClass"的itemscontrol创建了相同类型的多个控件(即Checkbox)。我已将复选框控件命名为" checkControl"。现在,由于我在UI中创建了多个复选框控件,我想检查它们的状态并区分它们。我该怎么办?我正在考虑使用findvisualchild& findvisualparent?但是,我不知道如何使用它?

<ItemsControl Name="myList">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="30,80,10,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="100" />
                </Grid.ColumnDefinitions>

                <Border x:Name="tempBorder" BorderBrush="LightGray" BorderThickness="2,2,2,2" CornerRadius="4,4,4,4" Margin="0,-30,-60,-30"
                                            Background="LightGray">
                    <StackPanel Orientation="Horizontal" Background="Transparent" Margin="0">
                        <StackPanel Background="White" Orientation="Horizontal">
                            <CheckBox x:Name="checkControl" Margin="7,7,0,0" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked">
                                <WrapPanel>
                                    <Image Source="/Images/myimage.png" Margin="0,10,0,0"></Image>
                                        <StackPanel Orientation="Vertical" VerticalAlignment="Center">
                                            <TextBlock Text="{Binding Disk}" FontFamily="roboto" FontSize="14"/>
                                            <Rectangle Width="340" Height="1" Margin="0,5,5,5" Fill="Black"/>
                                            <TextBlock>
                                                <Run Text="Item:" FontFamily="roboto" FontSize="14"/>
                                                <Run Text="{Binding Path=Name, StringFormat=' {0} Jr.'}" FontSize="20" Foreground="Orange"
                                                                 FontWeight="DemiBold"/>
                                            </TextBlock>
                                        </StackPanel>
                                    </WrapPanel>
                                </CheckBox>
                            </StackPanel>
                            <StackPanel Orientation="Vertical" Background="LightGray" Margin="0" HorizontalAlignment="Center" Width="765"
                                                        VerticalAlignment="Center">
                                <TextBlock Text="Select the Option:" Margin="15,7,0,7" FontFamily="roboto"/>
                                <ComboBox x:Name="comboControl" Margin="15,5,0,7" Width="750" SelectionChanged="comboControl_SelectionChanged"/>
                            </StackPanel>
                        </StackPanel>
                    </Border>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

对于我的后端C#代码: 类

 public class MyClass
{
    public string Disk { get; set; }

    public string Name { get; set; }

    public MyClass()
    {

    }

    public MyClass(string album, string name)
    {
        this.Disk = album;
        this.Name = name;
    }
}

在我的Xaml.cs

public ObservableCollection<MyClass> StudentDisk { get; set; }
//somecode
StudentDisk.Add(new MyClass("Disk 4 ", "John"));   //For populating
//somecode
myList.ItemsSource = StudentDisk;

1 个答案:

答案 0 :(得分:1)

可以通过DataContext区分checkBoxes,DataContext应该是唯一的:

void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    var checkbox = (CheckBox)sender;
    var item = (MyClass)checkbox.DataContext;
    MessageBox.Show(item.Disk + " " + item.Name);
}