Click here to Skip to main content
16,018,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A stack panel allows access from one user control to another user control but does not allow access to children of the stack panel.Rows that I add from the power_setting usercontrol inside the stackpanel won't let me access them from the Print_Setting usercontrol.From stackpanel power_setting is in UserControl


What I have tried:

this is my code for power_setting(User Control):-

C#
private void addbtn_Click(object sender, RoutedEventArgs e)
        {
            AddNewRow();
            rowCounter++;
        }
        private void AddNewRow()
        {
            var newRow = CreateRow();
            RowsStackPanel.Children.Add(newRow);
        }
        private StackPanel CreateRow()
        {
            var row = new StackPanel { Orientation = Orientation.Horizontal };
            var textBox1 = new TextBox { HorizontalAlignment = HorizontalAlignment.Left, Height = 23, Width = 70, Margin = new Thickness(1, 5, 5, 5), TextWrapping = TextWrapping.Wrap };
           
           
            // Event handler for textBox1 TextChanged event
            textBox1.TextChanged += (sender, e) =>
            {
                // Get the textbox control
                TextBox textbox = (TextBox)sender;
                // Check if the textbox name contains an underscore
                if (textbox.Name.Contains("_"))
                {
                    // Split the textbox name to extract the unique identifier
                    string uniqueId = textbox.Name.Split('_')[1];
                    // Store the textbox data in the registry
                    mr.Write("moveXtxt_" + uniqueId, textbox.Text);
                }
            };
            row.Children.Add(textBox1);
            return row;
        }



this is my code for Print_Setting usercontrol):-	


C#
private void PrintPowersetbtn_Click(object sender, RoutedEventArgs e)
        {

            //powersetting.OnButton2Clicked();
            //MessageBox.Show("OK");
            CompareValuesWithPowerText();
        }

        public void CompareValuesWithPowerText()
        {
            int targetValue = int.Parse(STL.valpower);
            var selectedRows = FindRowsWithMatchingValue(targetValue);
        }

        private List<StackPanel> FindRowsWithMatchingValue(int targetValue)
        {
            Power_Setting powersetting = new Power_Setting();
            var selectedRows = new List<StackPanel>();
            StackPanel closestLowerRow = null;
            StackPanel closestHigherRow = null;
            int closestLowerDiff = int.MaxValue;
            int closestHigherDiff = int.MaxValue;

            foreach (var child in powersetting.RowsStackPanel.Children)
            {
               // code 
            }

            if (closestLowerRow != null)
            {
                selectedRows.Add(closestLowerRow);
            }

            if (closestHigherRow != null)
            {
                selectedRows.Add(closestHigherRow);
            }

            return selectedRows;
        }
Posted
Updated 14-Jun-23 3:07am
v3
Comments
Graeme_Grant 14-Jun-23 8:32am    
The StackPanel control is a control collection layout control, it has no rows or columns, only display orientation - horizontal or vertical.

What are you wanting exactly?
Ridhdhi Vaghani 14-Jun-23 8:37am    
I want to access the children inside the stackpanel from the power_settings usercontrol to the print_settings usercontrol. please help me.
Graeme_Grant 14-Jun-23 8:43am    
You need a shared context. This can be done with a static class that both user controls have access to.
Ridhdhi Vaghani 14-Jun-23 8:44am    
Please give me some solution
Graeme_Grant 14-Jun-23 8:47am    
I am putting together an example. You will need to implement in your project.

1 solution

Create a new WPF project.

Add the following class:
C#
internal static class PowerSettingsContext
{
    public static Func<string, string> Find { get; set; }
}

This class will hold a delegate reference to the method in the UserControl that contains the StackPanel with the controls to search.

Here is that UserControl:
XML
<UserControl x:Class="WpfSharedContext.UserControl1"
             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" 
             xmlns:local="clr-namespace:WpfSharedContext"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <StackPanel x:Name="MyStackPanel" Orientation="Vertical">
            <TextBlock x:Name="TextBlock1"  Text="John"/>
            <TextBlock x:Name="TextBlock2"  Text="Paul"/>
            <TextBlock x:Name="TextBlock3"  Text="George"/>
            <TextBlock x:Name="TextBlock4"  Text="Ringo"/>
        </StackPanel>
    </Grid>
</UserControl>

And the code-behind:
C#
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        PowerSettingsContext.Find = FindControl;
    }

    public string FindControl(string text)
    {
        foreach (TextBlock child in MyStackPanel.Children.Cast<TextBlock>())
        {
            if (child.Text.Equals(text,
                    StringComparison.InvariantCultureIgnoreCase))
                return child.Name;
        }
        return "";
    }
}

The constructor points the shared PowerSettingsContext.Find method to the search method in the UserControl:
C#
PowerSettingsContext.Find = FindControl;

Now we can add the UserControl that will request the information:
XML
<UserControl x:Class="WpfSharedContext.UserControl2"
             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" 
             xmlns:local="clr-namespace:WpfSharedContext"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <StackPanel Margin="10"
                    Orientation="Horizontal"
                    Height="40">
            <TextBlock Text="Enter Value:"
                       VerticalAlignment="Center"/>
            <TextBox x:Name="SearchText" Width="100" Margin="10 0"/>
            <Button x:Name="SearchButton"
                    Padding="20 10" 
                    Content="GO!"
                    Click="SearchButton_OnClick"/>
        </StackPanel>

        <StackPanel Grid.Row="1" Margin="10" Orientation="Horizontal">
            <TextBlock Text="Result:"/>
            <TextBlock x:Name="Result" Width="100" Margin="10 0 0 0"/>
        </StackPanel>
    </Grid>
</UserControl>

and the code-behind:
C#
public partial class UserControl2 : UserControl
{
    public UserControl2()
    {
        InitializeComponent();
    }

    private void SearchButton_OnClick(object sender, RoutedEventArgs e)
    {
        Result.Text = PowerSettingsContext.Find(SearchText.Text);
    }
}

The Search Button Click will call the shared PowerSettingsContext.Find method and display the result. Here I am looking for the text and returning the control name that has the exact match.

Now we can add both UserControls to our host (window):
XML
<Grid Margin="20">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <local:UserControl1 />
    <local:UserControl2 Grid.Row="1" />
</Grid>

Now if you use any of the 4 names from UserControl1`, UserControl2 will show the control name that contains that text. Neither UserControl knows about the other, only the shared PowerSettingsContext class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900