Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中更改选项卡时如何刷新文本框文本_C#_.net_Wpf_Tabs_Textbox - Fatal编程技术网

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

C# 在WPF中更改选项卡时如何刷新文本框文本,c#,.net,wpf,tabs,textbox,C#,.net,Wpf,Tabs,Textbox,在我的WPF应用程序中,我使用的选项卡控件大约有5个选项卡。每个选项卡的视图都是我通过工具箱添加的用户控件 主Xaml文件: <Grid> <TabControl Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="tabControl1" VerticalAlignment="Stretch" Width="Auto"> <TabItem Header="Device C

在我的WPF应用程序中,我使用的选项卡控件大约有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>
<Grid>
    <Textbox Text="{Binding Box}", Name="hello" />
    // Some more textboxes
</Grid>
<Grid>
    <Textbox Text="{Binding I2CBox}", Name="helI2c" />
    // Some more textboxes
</Grid>
<Grid>
    <Textbox Text="{Binding VoltBox}", Name="heVoltllo" />
    // Some more textboxes
</Grid>**
<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>
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");
            }
        }
I2c.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>
<Grid>
    <Textbox Text="{Binding Box}", Name="hello" />
    // Some more textboxes
</Grid>
<Grid>
    <Textbox Text="{Binding I2CBox}", Name="helI2c" />
    // Some more textboxes
</Grid>
<Grid>
    <Textbox Text="{Binding VoltBox}", Name="heVoltllo" />
    // Some more textboxes
</Grid>**
<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>
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");
            }
        }

//更多的文本框
电压。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>
<Grid>
    <Textbox Text="{Binding Box}", Name="hello" />
    // Some more textboxes
</Grid>
<Grid>
    <Textbox Text="{Binding I2CBox}", Name="helI2c" />
    // Some more textboxes
</Grid>
<Grid>
    <Textbox Text="{Binding VoltBox}", Name="heVoltllo" />
    // Some more textboxes
</Grid>**
<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>
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");
            }
        }

//更多的文本框
**
默认情况下,我已将这些文本框的文本设置为某个值。让我们在我的视图模型类中分别说“12”、“13”和“14”。我的主要要求是设置每个用户控件中存在的这些文本框的文本,以便在我更改选项卡时刷新

说明:

<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>
<Grid>
    <Textbox Text="{Binding Box}", Name="hello" />
    // Some more textboxes
</Grid>
<Grid>
    <Textbox Text="{Binding I2CBox}", Name="helI2c" />
    // Some more textboxes
</Grid>
<Grid>
    <Textbox Text="{Binding VoltBox}", Name="heVoltllo" />
    // Some more textboxes
</Grid>**
<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>
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");
            }
        }
假设显示了连接视图:Textbox的值是12,我编辑它并将其更改为16。现在我点击I2C选项卡,然后返回连接选项卡,我希望文本框值刷新回初始值,即12

确切地说,它们是否是一个名为visibilitychanged()的方法,我可以在所有用户控件类中编写该方法,每当选项卡发生更改时,我都可以在其中设置这些Ui组件的值


请帮助:)

例如,确定。我们有简单的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>
像这样的。您可以调用Reinit方法,而不是设置textbox的值。
希望这会有所帮助。

您可以将数据绑定到选项卡控件“SelectedTab”或“SelectedIndex”属性。因此,当用户更改选项卡时,将调用视图模型中的setter,您可以在那里重置文本框属性绑定

编辑:

这是一个样本

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>
<Grid>
    <Textbox Text="{Binding Box}", Name="hello" />
    // Some more textboxes
</Grid>
<Grid>
    <Textbox Text="{Binding I2CBox}", Name="helI2c" />
    // Some more textboxes
</Grid>
<Grid>
    <Textbox Text="{Binding VoltBox}", Name="heVoltllo" />
    // Some more textboxes
</Grid>**
<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>
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");
            }
        }

您可以在view.xaml.cs文件中通过代码隐藏进行设置。@kuperspb:我想在我的viewmodel类中进行设置:)您知道如何进行设置吗?:)对如果mvvm库中有EventToCommand行为,则可以使用它。我们使用Catel mvvm,所以我们有这样一个方法:任何你需要在代码背后做一些工作的方法。我不想使用Catel。他们不是通用的方法吗???不。你的任务更接近于UI而不是ViewModel逻辑。非常感谢你的示例。这看起来很有希望。我不想改变文本框的文本。理想情况下,我会调用一个设置这些文本框值的方法:)您甚至可以调用ViewModel方法或更新ViewModel属性。但我认为这并不重要。无论如何,视图只修改用户过期的数据,不涉及视图模型逻辑和算法
XxxViewModel vm=tab2.DataContext作为XxxViewModel是的,这就是我要找的:)如果您能用代码详细说明,我将不胜感激:)请注意,文本框是我的用户控件的一部分,在我的解决方案中是一个单独的项目。你的意思是
resettextboxs()应该在我可以执行操作的不同用户控件的每个viewmodel类中调用???