Access to a child control of a user control

I created a user control consisting of an expander, a list, and checkboxes. I cannot access the checkboxes (child control) and I want to generate the number of expanders depending on the number of rows in the table dynamically. Can anyone suggest possible solutions for

+3
source share
4 answers

This is extremely vague. In most cases, you simply reveal some of the properties of internal control, for example. If you want to create dynamic content, you can set ItemsSourceand ItemTemplateinternal ListBoxthat you use, so it can be set from the outside, for example,

<UserControl x:Class="Test.UserControls.Bogus" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Name="control">
    <StackPanel>
        <TextBlock Text="Lorem Ipsum:" />
        <ItemsControl ItemsSource="{Binding ElementName=control, Path=ItemsSource}"
                ItemTemplate="{Binding ElementName=control, Path=ItemTemplate}" />
    </StackPanel>
</UserControl>
public partial class Bogus : UserControl
{
    public static readonly DependencyProperty ItemsSourceProperty = ItemsControl.ItemsSourceProperty.AddOwner(typeof(Bogus));
    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty ItemTemplateProperty = ItemsControl.ItemTemplateProperty.AddOwner(typeof(Bogus));
    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

    public Bogus()
    {
        InitializeComponent();
    }
}

:

<uc:Bogus ItemsSource="{Binding Data}">
    <uc:Bogus.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" Foreground="Red" />
        </DataTemplate>
    </uc:Bogus.ItemTemplate>
</uc:Bogus>

, , , .

, ItemsControl ( ListBox ), ItemTemplate, . , , .

+2

, . - Linq-to-VisualTree. CheckBoxes, 'this', :

IEnumerable<CheckBox> checks = this.Descendants<CheckBox>().Cast<CheckBox>();
0

. usercontrol Application.usercontrol.ComponentName, . , . BackgroundWorker.

, MainWindow

MainWindow rootWindow = Application.Current.MainWindow as MainWindow;

usercontrol :

rootWindow.usercontrolX.ComponentY.PropertyZ

0

. Parent User Control, , Child Child .

Parental control of the user. SingalData is a child of Contol

<my:C1TabItem Header="Signal">
        <local:SignalData Width="1036" OnSignalNameChange="SignalInputTab_OnSignalNameChange" Loaded="SignalInputTab_Loaded" Height="353" VerticalAlignment="Top" MinHeight="353" HorizontalAlignment="Left"></local:SignalData>

In the Contorl child class, if you have a component called tabProductList, you add the -

public C1.WPF.C1TabControl TabProductList
{
    get { return this.tabProductList; }
}

And finally, from your parent class, you can reference it as -

C1TabItem tbItem = (C1TabItem)c1TabControl1.SelectedItem;
SignalData sigInp = (SignalData)tbItem.Content;
if (sigInp.TabProductList.SelectedIndex == 0)
        {
....
-1
source

All Articles