如何从 StackPanel WPF 上以编程方式添加的 UC 获取价值
How to get value from programmatically added UC on StackPanel WPF
这是我的 UC
public partial class ChartDialogView : UserControl
{
public long Value
{
get
{
return TextBox1.Content;
}
}
public ChartDialogView()
{
InitializeComponent();
}
}
然后在主窗体上添加一些 UC 项目
ChartDialogView uc = new ChartDialogView();
ChartList.Children.Add(uc);
所以问题是如何获取第二个(例如)添加项的值?
这是我的XAML(也许有帮助)
<ScrollViewer Margin="44,40,0,11"
HorizontalAlignment="Left"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
Width="257"
Foreground="{x:Null}">
<StackPanel x:Name="ChartList">
<StackPanel.ScrollOwner>
<ScrollViewer CanContentScroll="True" />
</StackPanel.ScrollOwner>
</StackPanel>
</ScrollViewer>
所以如果有人需要,我找到了方法
foreach (ChartDialogView setInformation in ChartList.Children.OfType<ChartDialogView>())
{
string txt1 = setInformation.Name.Content.ToString() ;
MessageBox.Show(txt1);
}
这是我的 UC
public partial class ChartDialogView : UserControl
{
public long Value
{
get
{
return TextBox1.Content;
}
}
public ChartDialogView()
{
InitializeComponent();
}
}
然后在主窗体上添加一些 UC 项目
ChartDialogView uc = new ChartDialogView();
ChartList.Children.Add(uc);
所以问题是如何获取第二个(例如)添加项的值? 这是我的XAML(也许有帮助)
<ScrollViewer Margin="44,40,0,11"
HorizontalAlignment="Left"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
Width="257"
Foreground="{x:Null}">
<StackPanel x:Name="ChartList">
<StackPanel.ScrollOwner>
<ScrollViewer CanContentScroll="True" />
</StackPanel.ScrollOwner>
</StackPanel>
</ScrollViewer>
所以如果有人需要,我找到了方法
foreach (ChartDialogView setInformation in ChartList.Children.OfType<ChartDialogView>())
{
string txt1 = setInformation.Name.Content.ToString() ;
MessageBox.Show(txt1);
}