简体   繁体   English

如何在WPF中更改选项卡时刷新文本框文本

[英]How to refresh the textbox text when tabs are Changed in WPF

Well in my WPF application I am using Tab Control which has around 5 tabs. 在我的WPF应用程序中,我正在使用Tab Control,它有大约5个选项卡。 The view of each tab is a user control which I add via a tool box. 每个选项卡的视图是一个用户控件,我通过工具箱添加。

Main Xaml File: 主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>

If you notice each view ie.e Connect , I2C and Voltage is a user control which has a view, viewmodel and model class :) 如果您发现每个视图ie.e ConnectI2CVoltage是具有视图,视图模型和模型类用户控制:)

Each of these views have set of textboxes in their respective xaml files. 这些视图中的每一个都在其各自的xaml文件中具有一组文本框。

Connect.xaml: Connect.xaml:

<Grid>
    <Textbox Text="{Binding Box}", Name="hello" />
    // Some more textboxes
</Grid>

I2c.xaml: I2c.xaml:

<Grid>
    <Textbox Text="{Binding I2CBox}", Name="helI2c" />
    // Some more textboxes
</Grid>

Voltage.xaml: Voltage.xaml:

<Grid>
    <Textbox Text="{Binding VoltBox}", Name="heVoltllo" />
    // Some more textboxes
</Grid>**

By default I have set the text of these textboxes to some value. 默认情况下,我已将这些文本框的文本设置为某个值。 Lets say "12" "13" "14" respectively in my view model classes. 让我在视图模型类中分别说“12”“13”“14”。 My main requirement is to set the text of these textboxes present in each user control to get refreshed when I change the tab. 我的主要要求是在更改选项卡时设置每个用户控件中存在的这些文本框的文本以刷新。

Description: 描述:

Lets say Connect View is displayed: Value of Textbox is 12 and I edit it and change it to 16. Now I click on I2C tab and then I go back to Connect tab, I want the textbox value to get refreshed back to the initial value ie 12. 让我们说连接视图显示:文本框的值是12,我编辑它并将其更改为16.现在我单击I2C选项卡,然后我返回到连接选项卡,我希望文本框值刷新回初始值即12。

To be precise, is their a method called visibilitychanged() which I can write in all my user control classes, where I can set the value of these Ui components whenever tabs are changed? 确切地说,是一个名为visibilitychanged()的方法,我可以在所有用户控件类中编写,我可以在每次更改选项卡时设置这些Ui组件的值吗?

Please help :) 请帮忙 :)

Ok, for example. 好的,例如。 We have simple WPF app. 我们有简单的WPF应用程序。 Main window: 主窗口:

<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 is just default template from VS, so no code here. Tab1只是VS的默认模板,所以这里没有代码。 Tab2: 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>

As you can see we have event handler for TabControl_SelectionChanged event. 如您所见,我们有TabControl_SelectionChanged事件的事件处理程序。 In code behind of mainwindow we have: 在主窗口的代码中,我们有:

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";
    }
}

Something like this. 像这样的东西。 You can call Reinit method instead of setting value of textbox. 您可以调用Reinit方法而不是设置文本框的值。 Hope this will help. 希望这会有所帮助。

You can do data binding to the TabControl "SelectedTab" or "SelectedIndex" properties. 您可以对TabControl“SelectedTab”或“SelectedIndex”属性进行数据绑定。 so when ever user changes tab your setter in view model will be called, there you can reset the text box property bindings.. 因此,当用户更改选项卡时,将调用视图模型中的setter,您可以重置文本框属性绑定。

Edit: 编辑:

Here is a sample 这是一个例子

XAML: 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>

Properties and Method In ViewModel: 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");
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM