datatemplate中的Usercontrol不同步

时间:2012-02-22 15:26:50

标签: c# wpf binding user-controls datatemplate

所以我希望有多个带有usercontrol的选项卡,具体取决于列表中有多少项。所以我认为这太难了......但我开始用一个新窗口制作一个tabcontroller并将其itemsource(tabcontroler的列表)绑定到列表中:

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

一旦窗口加载,它只会执行tabcontrol.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:

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

对于这么多代码感到很抱歉,但是代码太少可能会更好。问题是我希望按钮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方法应该改变它吗? 对不起这个LONG问题,但是我一直在为这个问题搞砸了,没有运气。

SOLUTION:

在usercontrol中用Text =“{Binding Path = code}”替换Text =“{Binding Path = strcode,RelativeSource = {RelativeSource AncestorType = {x:Type UserControl}}}”。并将INotifyPropertyChanged添加到clc类,Thx to Rachel!

1 个答案:

答案 0 :(得分:2)

这里有两个问题。

首先,你的类没有实现INotifyPropertyChanged,所以UI不知道它的对象已经改变了。让您的clc类实现此接口,并在PropertyChangedcode属性发生更改时引发name事件,以便UI知道更新。

第二个问题是默认情况下,如果可能,WPF将重新使用模板。例如,您的TabControl正在创建UserControl的一个实例,当您切换标签时,只需更改UserControl后面的DataContext即可。如果txtclc.Text未绑定DataContext,则它不会更改,因为您正在查看完全相同的TextBox,无论您正在查看哪个标签。解决方案是向DataContext对象添加属性以存储Text TextBox的txtclc,并绑定属性。