C# XAML中的数据绑定(多路绑定?)
假设我有一个列表框,它绑定到代码隐藏中的内容:C# XAML中的数据绑定(多路绑定?),c#,.net,wpf,xaml,binding,C#,.net,Wpf,Xaml,Binding,假设我有一个列表框,它绑定到代码隐藏中的内容: <ListBox x:Name="list"> <ListBox.ItemTemplate> <DataTemplate> <ListBoxItem Content="{Binding Name}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> &l
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem Content="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox x:Name="name" Text="{Binding ElementName=list, Path=SelectedItem.Name, Mode=TwoWay" />
<TextBox x:Name="contents" Text="{Binding ElementName=list, Path=SelectedItem.Contents, Mode=TwoWay" />
现在,上面的内容正是我想要的。选择列表框中的项目时,文本框将更新以显示列表框中选择的内容
但我现在要做的是通过在Dude类中添加字典来扩展它:
public class Dude
{
public string Name { get; set; }
public string Contents { get; set; }
public Dictionary<String, String> Tasks { get; set; }
}
public class Dude
{
公共字符串名称{get;set;}
公共字符串内容{get;set;}
公共字典任务{get;set;}
}
希望我能:
单击列表框中的某个项目,并具有相应项目的名称和名称
内容属性显示在各自的文本框中,然后
将字典的
内容
但我不知道我怎么能走那么深。有点像我要去多个层次,像多维绑定这样的东西是我需要的吗
你有没有(简单的)样品,或者见过?文档、文章、教程
非常感谢您的帮助
谢谢你你想要的东西可以在WPF中完成(在其他XAML技术如Wp7或WinRT中要难一点),但是我不确定这是否是你所需要的 使用多重绑定,将内容字符串和任务字典绑定到第二个文本框,然后编写自己的IMultiValueConverter以生成要显示的字符串 请阅读,粗略的代码应该如下所示:
<TextBox>
<TextBox.Text>
<MultiBinding Converter="{StaticResource YourAppendingConverter}">
<Binding ElementName="list" Path="SelectedItem.Contents" />
<Binding ElementName="list" Path="SelectedItem.Tasks" />
</MultiBinding>
</TextBox.Text>
</TextBox>
您的转换器应该类似于:
public class YourAppendingConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture){
StringBuilder sb = new StringBuilder(values[0].ToString());
sb.AppendLine("Tasks:");
foreach (var task in (Dictionary<string,string>)values[1]){
sb.AppendLine(string.Format("{0}: {1}", task.Key, task.Value));
}
return sb.ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture){
throw new NotSupportedException();
}
公共类YourAppendingConverter:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,
System.Globalization.culture(信息文化){
StringBuilder sb=新的StringBuilder(值[0].ToString());
某人追加(任务:);
foreach(字典中的var任务值[1]){
AppendLine(string.Format(“{0}:{1}”,task.Key,task.Value));
}
使某人返回字符串();
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,
System.Globalization.culture(信息文化){
抛出新的NotSupportedException();
}
为什么我认为这是你需要的
如果您仔细考虑这些问题,那么多重绑定可能是您所需要的工具。我认为更好的解决方案可能包括一个列表框或其他ItemsControl,您可以将其绑定到Tasks属性,而不是将Tasks对附加到已绑定到Contents属性的控件。但是,我已经很久没有完成XAML绑定了o恐怕我现在没有更好的答案给你。祝你好运!这可能有用。你的字典必须是静态的,或者我看不到它的用处。你可以做的是更改绑定,使其在目标更新时通知,然后捕获目标更新事件。()哇,非常感谢@TDaver。在这种情况下,我真的很怀念WinForms的简单性。@ProtectedIdentity:我发现这更符合逻辑:)
public class YourAppendingConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture){
StringBuilder sb = new StringBuilder(values[0].ToString());
sb.AppendLine("Tasks:");
foreach (var task in (Dictionary<string,string>)values[1]){
sb.AppendLine(string.Format("{0}: {1}", task.Key, task.Value));
}
return sb.ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture){
throw new NotSupportedException();
}