在我的WPF应用程序中,我正在使用Tab Control,它有大约5个选项卡。每个选项卡的视图是一个用户控件,我通过工具箱添加。
主要Xaml文件:
<Grid>
<TabControl Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="tabControl1" VerticalAlignment="Stretch" Width="Auto">
<TabItem Header="Device Control" Name="Connect">
<ScrollViewer Height="Auto" Name="scrollViewer1" Width="Auto">
<my:ConnectView Name="connectView1" />
</ScrollViewer>
</TabItem>
<TabItem Header="I2C">
<ScrollViewer Height="Auto" Name="scrollViewer2" Width="Auto">
<my1:I2CControlView Name="i2CControlView1" />
</ScrollViewer>
</TabItem>
<TabItem Header="Voltage">
<ScrollViewer Height="Auto" Name="scrollViewer3" Width="Auto">
<my2:VoltageView Name="voltageView1" />
</ScrollViewer>
</TabItem>
</TabControl>
</Grid>
如果您注意到每个视图ie.e Connect
,I2C
和Voltage
是一个具有视图,视图模型和模型类的用户控件:)
这些视图中的每一个都在各自的xaml文件中设置了一组文本框。
Connect.xaml:
<Grid>
<Textbox Text="{Binding Box}", Name="hello" />
// Some more textboxes
</Grid>
I2c.xaml:
<Grid>
<Textbox Text="{Binding I2CBox}", Name="helI2c" />
// Some more textboxes
</Grid>
Voltage.xaml:
<Grid>
<Textbox Text="{Binding VoltBox}", Name="heVoltllo" />
// Some more textboxes
</Grid>**
默认情况下,我已将这些文本框的文本设置为某个值。让我在视图模型类中分别说“12”“13”“14”。我的主要要求是设置每个用户控件中存在的这些文本框的文本,以便在我更改选项卡时刷新。
说明
让我们说连接视图显示:文本框的值是12,我编辑它并将其更改为16.现在我单击I2C选项卡,然后我返回到连接选项卡,我希望文本框值刷新回来初始值即12.
确切地说,是一个名为visibilitychanged()的方法,我可以在所有用户控件类中编写,我可以在更改选项卡时设置这些Ui组件的值吗?
请帮助:)
答案 0 :(得分:2)
好的,例如。我们有简单的WPF应用程序。 主窗口:
<Window x:Class="tabs.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:tabs"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl SelectionChanged="TabControl_SelectionChanged">
<TabItem Header="1">
<my:Tab1/>
</TabItem>
<TabItem Header="2">
<my:Tab2/>
</TabItem>
</TabControl>
</Grid>
</Window>
Tab1只是VS的默认模板,所以这里没有代码。 TAB2:
<UserControl x:Class="tabs.Tab2"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="120" Text="asd" />
</Grid>
</UserControl>
正如您所看到的,我们有TabControl_SelectionChanged事件的事件处理程序。在mainwindow后面的代码中我们有:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl)
{
TabItem tabitem = e.AddedItems[0] as TabItem;
if (tabitem == null)
return;
Tab2 tab2 = tabitem.Content as Tab2;
if (tab2 == null)
return;
tab2.textBox1.Text = "zxczxczxczxc";
}
}
像这样的东西。您可以调用Reinit方法而不是设置文本框的值。 希望这会有所帮助。
答案 1 :(得分:1)
您可以对TabControl“SelectedTab”或“SelectedIndex”属性进行数据绑定。因此,当用户更改选项卡时,将调用视图模型中的setter,您可以重置文本框属性绑定..
编辑:
这是一个示例
<强> XAML:强>
<TabControl SelectedIndex="{Binding TabIndex}">
<TabItem Header="Tab 1">
<TextBox Text="{Binding TextValue1}" Height="20" Width="200"></TextBox>
</TabItem>
<TabItem Header="Tab 2">
<TextBox Text="{Binding TextValue2}" Height="20" Width="200"></TextBox>
</TabItem>
<TabItem Header="Tab 3">
<TextBox Text="{Binding TextValue3}" Height="20" Width="200"></TextBox>
</TabItem>
</TabControl>
ViewModel中的属性和方法:
private int tabIndex;
public int TabIndex
{
get { return tabIndex; }
set
{
tabIndex = value;
NotifyPropertyChanged("TabIndex");
ResetTextBoxes();
}
}
private void ResetTextBoxes()
{
TextValue1 = "12";
TextValue2 = string.Empty;
TextValue3 = "default";
}
private string textValue1;
public string TextValue1
{
get { return textValue1; }
set
{
textValue1 = value;
NotifyPropertyChanged("TextValue1");
}
}
private string textValue2;
public string TextValue2
{
get { return textValue2; }
set
{
textValue2 = value;
NotifyPropertyChanged("TextValue2");
}
}
private string textValue3;
public string TextValue3
{
get { return textValue3; }
set
{
textValue3 = value;
NotifyPropertyChanged("TextValue3");
}
}