2012-10-22 51 views
1

那麼在我的WPF應用程序中,我使用的Tab控件具有大約5個選項卡。每個選項卡的視圖都是通過工具箱添加的用戶控件。如何刷新標籤頁時的文本框文本在WPF中更改

主要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 ConnectI2CVoltage是具有視圖,視圖模型和模型類:)

每個用戶控制這些視圖在其各自的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

您可以通過代碼來設置它的背後,在view.xaml中.cs文件。 – kuperspb

+0

@kuperspb:我想做到這一點在我的ViewModel類:)你知道我們如何能做到這樣? :) – StonedJesus

+0

是的。你可以使用EventToCommand行爲,如果你在你的mvvm庫中。我們使用Catel MVVM,所以我們有這樣的: http://catel.catenalogic.com/index.htm?behaviors_eventtocommand.htm 你需要做的背後代碼一些工作任何關係。 – kuperspb

回答

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事件的事件處理程序。在主窗口後面的代碼中,我們有:

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方法而不是設置文本框的值。 希望這會有所幫助。

+0

感謝很多的例子。這看起來有希望。那麼我不想改變文本文本。我會理想地調用一個方法來設置這些文本框的值:) – StonedJesus

+0

你甚至可以調用你的ViewModel方法或更新ViewModel屬性。但我認爲這並不重要。無論如何,view僅修改用戶expirience的數據,而不涉及視圖模型邏輯和算法。 'XxxViewModel vm = tab2.DataContext as XxxViewModel;' – kuperspb

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> 

屬性和方法視圖模型:

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"); 
      } 
     } 
+0

是啊,這是我一直在尋找:)如果你能詳細說明一下代碼,請諒解:) – StonedJesus

+0

請注意,文本框是我的用戶控件的一部分,它是單獨的項目在我的解決方案所以你的意思是'ResetTextBoxes();'應該在我可以執行操作的不同用戶控件的每個viewmodel類中調用? – StonedJesus