我之前尝试过这个:
string st = "Unit";
foreach (Label lb in tabControl1.Items)
{
if (st.IndexOf(lb.Name) != -1)
lb.Content = "Some text";
}
没用...... 谢谢你的帮忙 编辑:我确实使用.text而不是.content抱歉。
答案 0 :(得分:1)
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
private string textdisplayed;
public string Textdisplayed
{
get { return textdisplayed; }
set
{
textdisplayed = value;
InvokePropertyChanged(new PropertyChangedEventArgs("Textdisplayed"));
}
}
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
#endregion
}
在任何地方更改文本只需将属性Textdispalyed
绑定到XAML中的标签即可。
<TextBlock Text="{Binding Path=Textdisplayed,
ElementName=mainWindow}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36"></TextBlock>
答案 1 :(得分:0)
编辑:
请改用此方法:
var control = this.FindName("button1");
但是如果你还想要搜索每一件物品
你可以把每个孩子都投射到一个控制器上然后做你想做的事情
var container =((tabControl1.Items [0] as TabItem).Content as Panel);
foreach(容器中的控制控制) { //做你想做的事 }