开发者

Access the child control of a user control

开发者 https://www.devze.com 2023-03-10 20:53 出处:网络
I have created a user control that consists of a expander, listbox and checkboxes. I am not able to access the checkboxes (child control) and I wa开发者_C百科nt to generate the number of expanders bas

I have created a user control that consists of a expander, listbox and checkboxes. I am not able to access the checkboxes (child control) and I wa开发者_C百科nt to generate the number of expanders based on the number of rows in a table dynamically. Can anyone suggest the possible solutions to


This is extremely vague. In most cases you would just expose some of the internal control's properties, e.g. if you want to create dynamic content you would expose the ItemsSource and ItemTemplate of an internal ListBox of whatever you use so it can be set from outside, e.g.

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

Usage:

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

You can of course also encapsulate a lot of logic which you do not need exposed.

Since you want a varying amount of expanders you might have an ItemsControl (unlike a ListBox it has no selection) which already defines an ItemTemplate which contains an expander. You probably can also create a partial template as shown in this answer of mine.


Sounds like you need to navigate the visual tree. The simplest way of doing this is via Linq-to-VisualTree. To find all the CheckBoxes that are a child of 'this', use the following query:

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


Your application is running in an Application instance. Access the usercontrol components with Application.usercontrol.ComponentName if it is not a UI update. If you make UI updates, you have to run the access in a separate dispatcher thread. In that case, use BackgroundWorker.

For example, I am running my main application class MainWindow and accessing it as,

MainWindow rootWindow = Application.Current.MainWindow as MainWindow;

Now access the usercontrol and properties of components as:

rootWindow.usercontrolX.ComponentY.PropertyZ


Define properties in the child's class for each of those controls. You will be able to access them from the Parent User Control, assuming you have added the Child User Control within the Parent User Control.

Parent User Control.. SingalData is the child User 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 Child User Contorl class, if you have a component named tabProductList you add a property -

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)
        {
....
0

精彩评论

暂无评论...
验证码 换一张
取 消