我正在尝试将datatemplates与DataTemplateSelector一起使用。我想在程序运行时选择2个模板中的一个。
所以我有2个简单的模板:
<DataTemplate x:Key="User_Text_Message">
<StackPanel>
<TextBlock Text="{Binding UsCom}" Foreground="Black"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="Anastasia_Text_Message">
<StackPanel>
<TextBlock Text="{Binding Answer}" Foreground="Yellow"/>
</StackPanel>
</DataTemplate>
<t:MessagesAdd
User_Text_Message="{StaticResource User_Text_Message}"
Anastasia_Text_Message="{StaticResource Anastasia_Text_Message}"
x:Key="MessAdd"/>
ListView
<ListView x:Name="MainChat" Height="330" Background="{x:Null}"
Margin="10,25,0,10" HorizontalAlignment="Left"
Width="680" Foreground="White" ItemTemplateSelector="{StaticResource MessAdd}"
ItemsSource="{Binding MessagesAdd}">
</ListView>
和代码
class MessagesAdd : DataTemplateSelector
{
public DataTemplate Anastasia_Text_Message { get; set; }
public DataTemplate User_Text_Message { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
switch(MainLogic.MainVars.TemplateType)
{
case 1:
var g = new UserTextCommand() { UsCom = MainLogic.MainVars.UserCommand };
MainWindow.Instance.MainChat.Items.Add(g);
return User_Text_Message;
case 2:
var a = new AnastasiaTextAnswer() { Answer = MainLogic.MainVars.AnastasiaAnswer };
MainWindow.Instance.MainChat.Items.Add(a);
return Anastasia_Text_Message;
default:
return null;
}
}
}
class UserTextCommand
{
public string UsCom { get; set; }
}
class AnastasiaTextAnswer
{
public string Answer { get; set; }
}
然后我输入“Hello”并单击SendButton
public void AnswersADD()
{
MainLogic.MainVars.TemplateType = 1;
add.SelectTemplate(null, null);
MainLogic.MainVars.TemplateType = 2;
add.SelectTemplate(null, null);
}
我拥有的。没有我的信息,只有很多答案。
不能理解我必须做的事情。
现在我这样做
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
ObservableCollection<AnswersLogic.AnastasiaTextAnswer> answer = new ObservableCollection<AnswersLogic.AnastasiaTextAnswer>();
ObservableCollection<AnswersLogic.UserTextCommand> usercom = new ObservableCollection<AnswersLogic.UserTextCommand>();
if (item is UserTextCommand)
{
usercom.Add(new UserTextCommand() { UsCom = MainLogic.MainVars.UserCommand});
MainWindow.Instance.MainChat.ItemsSource = usercom;
return User_Text_Message;
}
else if (item is AnastasiaTextAnswer)
{
answer.Add(new AnastasiaTextAnswer() { Answer = MainLogic.MainVars.AnastasiaAnswer });
MainWindow.Instance.MainChat.ItemsSource = answer;
return Anastasia_Text_Message;
}
else
{
return null;
}
}
}
但
中有无限循环 MainWindow.Instance.MainChat.ItemsSource = usercom;
和
MainWindow.Instance.MainChat.ItemsSource = answer;
答案 0 :(得分:0)
什么是MainLogic.MainVars.TemplateType?根据传递给SelectTemplate方法的项目,我不知道这会有什么不同。另外,我不确定你为什么要在这个选择器中添加东西。它应该提供一个模板,就是这样。像这样:
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is UserTextCommand)
{
return User_Text_Message;
}
else if (item is AnastasiaTextAnswer)
{
return Anastasia_Text_Message;
}
else
{
return null;
}
}
您正在获取所有相同模板的项目,因为MainLogic.MainVars.TemplateType不会更改。您获得的项目数量超出预期,因为您在选择器中添加了更多项目。