C# datatemplate中的Usercontrol不同步
因此,我希望有多个选项卡,其中包含一个usercontrol,具体取决于列表中有多少项。所以我认为这不应该太难,但是。。。。我从一个新窗口开始创建一个tabcontroller,并将其itemsource(tabcontroller的)绑定到列表: 它确实改变了它!我可以通过debug.writeline看到clclist中的值已更改。但此选项卡中的listview不会更改!只有当我转到另一个标签,然后回到第一个标签,那么它已经与正确的冠军改变。甚至还有第二个问题。O但是如果我转到其他选项卡(usercontrol),txtclc.text也会更改为第一个选项卡!此外,该列表不会在usercontrol上更新,也不会在!但是makelist方法应该改变它吗? 很抱歉问了这么长的问题,但是我一直在谷歌上搜索,没有解决这个问题的运气 解决方案:C# datatemplate中的Usercontrol不同步,c#,wpf,binding,user-controls,datatemplate,C#,Wpf,Binding,User Controls,Datatemplate,因此,我希望有多个选项卡,其中包含一个usercontrol,具体取决于列表中有多少项。所以我认为这不应该太难,但是。。。。我从一个新窗口开始创建一个tabcontroller,并将其itemsource(tabcontroller的)绑定到列表: 它确实改变了它!我可以通过debug.writeline看到clclist中的值已更改。但此选项卡中的listview不会更改!只有当我转到另一个标签,然后回到第一个标签,那么它已经与正确的冠军改变。甚至还有第二个问题。O但是如果我转到其他选项卡(u
将用户控件中的Text=“{Binding Path=strcode,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}}”替换为Text=“{Binding Path=code}”。并将INotifyPropertyChanged添加到clc类,Thx添加到Rachel 这里有两个问题 首先,您的类没有实现,因此UI不知道它的对象已经更改。使您的
clc
类实现此接口,并在code
和name
属性更改时引发PropertyChanged
事件,以便UI知道要更新
第二个问题是,默认情况下,如果可能,WPF将重用模板。例如,您的
TabControl
正在创建UserControl
的一个实例,当您切换选项卡时,它只是更改UserControl后面的DataContext
。如果txtclc.Text
未绑定到DataContext
中的某个内容,则它不会更改,因为无论您查看的是哪个选项卡,您都在查看完全相同的TextBox
。解决方案是向DataContext
对象添加一个属性,以存储txtclc
文本框的文本,并绑定该属性。Ty!我实现了INotifyPropertyChanged,现在UI正在按应有的方式更新!:)然而,对于我的第二个问题,您也提供了答案,我无法理解您的意思:向我的datacontext添加一个属性来存储txtclc的文本。我有一个Datacontext吗。我现在就用谷歌搜索你可能的意思,但再多了解一点我必须要做的事情将是非常甜蜜的。不过,已经谢谢你了!:)@Maximc在您的例子中,DataContext
看起来像是objectclc
的一个实例,因此您需要在那里添加属性。我记得我写过一个小例子,说明DataContext是如何在某处工作的。。。。“让我看看我能不能找到它。”马克西姆找到了。请查看我的答案,但我不是已经这么做了,我正在将它保存在clc类的属性字符串“code”中?这就是我如何能够在开始时在文本框中获取clccode的方法(是的,对于每个选项卡,textbox.text都是不同的(但只有在开始时,我插入其他文本并将其保存到clc类,然后它才是相同的)?无论如何,我现在已经准备好了你在那里的帖子,如果我“仍然”没有得到它,我将发布:)@Maximc您正在绑定到UserControl
上的一个属性,由于UserControl
是同一个实例,无论您在哪个选项卡上,该属性都保持不变。如果要将其绑定到DataContext.code
而不是strcode
,则会得到不同的值,因为更改选项卡时,DataContext
会更改。
<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>
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>
private void btnsave_Click(object sender, RoutedEventArgs e)
{
settings.clclist[teller].code = txtclc.Text;
settings.clclist[teller].makelist();
}