2012-02-22 35 views
0

所以我想擁有多個帶有usercontrol的選項卡,具體取決於列表中有多少項。所以我覺得這shoudn T爲太辛苦,但....我開始一個新的窗口,使tabcontroller並結合其的ItemSource(該tabcontroler到列表中的):datatemplate中的用戶控件不同步

<Window x:Class="xxxxx.extraforms.frmownedchamps" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:sp="clr-xxxxx.usercontrols.ucoptions" 
     Title="frmownedchamps" Height="593" Width="350" Loaded="Window_Loaded_1" ResizeMode="NoResize" WindowStyle="None" ShowInTaskbar="False"> 
    <Grid> 
     <TabControl Name="thetabcontrol"> 
      <TabControl.ItemTemplate> 
       <DataTemplate> 

        <StackPanel> 
         <Label Content="{Binding name}" /> 
        </StackPanel> 

       </DataTemplate> 
      </TabControl.ItemTemplate> 

      <TabControl.ContentTemplate> 
       <DataTemplate DataType="{x:Type sp:ucownedchampviewer}" > 
        <sp:ucownedchampviewer strname="{Binding Path=name}" strcode="{Binding Path=code}" clclist="{Binding Path=list}" teller="{Binding Path=teller}" /> 
       </DataTemplate> 
      </TabControl.ContentTemplate> 
     </TabControl> 

    </Grid> 
</Window> 

一旦窗口得到加載它只有thetabcontrol.ItemsSource = settings.clclist;

的clclist是這樣的:

 public static List<clc> clclist { get; set; } 

    public void methodblabla() 
    { 
    foreach(xml blabla) 
    { 
    clc clctemp = new clc(xmlname, xmlcode); 
    clclist.Add(clctemp); 
    } 
    } 

the clc class is: 

public class clc 
     { 
      private static int counter = 0; 
      public int teller { get; set; } 
      public String name { get; set; } 
      public String code { get; set; } 
      public ObservableCollection<champion> list { get; set; } 
      public clc(String name, String code) 
      { 

       this.name = name; 
       this.code = code; 
       teller = counter; 
       counter++; 
       makelist(); 
      } 
      public void makelist() 
      { 
       var bytes = Convert.FromBase64String(code); 
       var values = new System.Collections.BitArray(bytes); 
       list = new ObservableCollection<champion>(); 
       int aantal = champions.list.Count; 
       int teller = 0; 
       int counter = 0; 
       for (int x = aantal; x != 0; x--) 
       { 
        if (values[x - 1] == true) 
        { 
         list.Add(champions.getchampbyid(counter + 1)); 
         teller++; 
        } 
        counter++; 
       } 
      } 
     } 

我的用戶:

<UserControl x:Class="xxxxx.usercontrols.ucoptions.ucownedchampviewer" 
      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" 
       xmlns:sp="clr-namespace:xxxxx" 
      mc:Ignorable="d" 
      d:DesignHeight="564" d:DesignWidth="350" Loaded="UserControl_Loaded"> 
    <Grid Height="624"> 
     <Grid.Resources> 
      <Style x:Key="Ownedstyle" TargetType="{x:Type ListViewItem}"> 
       <Setter Property="Background" Value="Red"></Setter> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding strowned}" Value="Yes"> 
         <Setter Property="Background" Value="Green"></Setter> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </Grid.Resources> 
      <StackPanel Margin="0,0,0,12"> 
      <StackPanel Orientation="Horizontal" Margin="5"> 
       <TextBox Name="txtclc" Width="250" Margin="2" Text="{Binding Path=strcode ,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" /> 
       <Button Name="btnload" Content="Save" Click="btnsave_Click" Width="55" Margin="2"/> 
      </StackPanel> 
      <Line Margin="2" /> 
      <StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Stretch" Width="350"> 
       <TextBlock VerticalAlignment="Center">Filter:</TextBlock> 
       <TextBox Name="txtfilter" Height="30" Grid.Column="0" TextChanged="txtfilter_TextChanged" Margin="5" Width="250" /> 
       <Label Name="lblaantal"></Label> 
      </StackPanel> 
      <ListView Name="lsvallchamps" Grid.Column="0" Grid.Row="1" Grid.RowSpan="1" Height="410" Width="auto" ItemContainerStyle="{StaticResource Ownedstyle}" ItemsSource="{Binding Path=clclist ,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" > 
       <ListView.View> 
        <GridView> 
         <GridViewColumn Width="60"> 
          <GridViewColumn.CellTemplate> 
           <DataTemplate> 
            <Image HorizontalAlignment="Center" VerticalAlignment="Center" Width="50" Height="50" Source="{Binding image}" Stretch="Fill"></Image> 
           </DataTemplate> 
          </GridViewColumn.CellTemplate> 
         </GridViewColumn> 
         <GridViewColumn Width="120" DisplayMemberBinding="{Binding name}"> 
          <GridViewColumnHeader Content="name" Tag="name" Click="SortClick"/> 
         </GridViewColumn> 
         <GridViewColumn Width="130" DisplayMemberBinding="{Binding strroles}"> 
          <GridViewColumnHeader Content="strroles" Tag="strroles" Click="SortClick" /> 
         </GridViewColumn> 
        </GridView> 
       </ListView.View> 
      </ListView> 
      <Button Content="testknop" Click="Button_Click" /> 
      <Button Content="Hide" Name="btnhide" Width="150" Height="35" Margin="5" Click="btnhide_Click"></Button> 
     </StackPanel> 
    </Grid> 
</UserControl> 

對不起這麼多的代碼,但mayby更好太多太然後更少的代碼。問題是,我希望按鈕btnsave到txtclc.text保存到代碼,使它的一個新的列表,(然後將列表視圖應改爲全自動根據它,因爲它被綁定到它) 然而,一旦我使用代碼

private void btnsave_Click(object sender, RoutedEventArgs e) 
     { 
      settings.clclist[teller].code = txtclc.Text; 
      settings.clclist[teller].makelist(); 
     } 

它確實改變了它!我可以用debug.writeline看到它在clclist中的值發生了變化。但在這個標籤中的列表視圖不會改變!只有當我去到另一個選項卡,然後回到第一個,然後它改變了正確的冠軍。甚至還有第二個問題。但是,如果我轉到另一個選項卡(usercontrol),則txtclc.text也會更改爲第一個!此外,該列表不會在usercontrol上更新!然而makelist方法應該改變它? 對不起,這個長問題,但我已經googeling很多,沒有運氣這個問題。

SOLUTION:

替換文本= 「{綁定路徑= strcode,的RelativeSource = {的RelativeSource AncestorType = {X:類型用戶控件}}}」 使用文本= 「{綁定路徑=代碼}」 中的用戶控件。並將INotifyPropertyChanged添加到clc類,將Thx添加到Rachel!

回答

2

您這裏有兩個問題。

首先,是你的類沒有實現INotifyPropertyChanged,所以UI不知道,它的對象發生了變化。使clc類實現此接口,並在codename屬性發生更改以便UI知道要更新時提高PropertyChanged事件。

第二個問題是,默認情況下,如果可能,WPF將重新使用模板。例如,您TabControl是創建UserControl的一個實例,當你切換標籤它只是改變了用戶控件背後的DataContext。如果txtclc.Text未綁定到DataContext中的某個內容,則不會因爲您正在查看完全相同的TextBox而不管您正在查看哪個選項卡。該解決方案是將屬性添加到您的DataContext對象存儲txtclc TextBox的Text,並綁定屬性。

+0

泰這個!我實現了INotifyPropertyChanged,現在UI正在更新,因爲它應該! :)但是,對於我還提供了一個答案的第二個問題,我無法弄清楚你的意思:將一個屬性添加到我的datacontext中以存儲txtclc的文本。我是否有Datacontext lol:p。我會谷歌現在你可能意味着什麼,但更多的信息,我興奮地必須做的將是非常甜蜜。不過,謝謝你!:) – Maximc 2012-02-22 16:29:53

+0

@Maximc在你的情況下,'DataContext'看起來像是一個對象'clc'的實例,所以你需要在那裏添加屬性。我記得寫了一個DataContext如何工作的小例子....讓我看看我能否找到它。 – Rachel 2012-02-22 16:34:38

+0

@Maximc找到它。看看我的答案[這裏](http://stackoverflow.com/a/7262322/302677) – Rachel 2012-02-22 16:35:08

相關問題