绑定到WPF中的Uri
我正在将URL列表绑定到列表框(MVVM),发现如果模型是绑定到WPF中的Uri,wpf,data-binding,mvvm,Wpf,Data Binding,Mvvm,我正在将URL列表绑定到列表框(MVVM),发现如果模型是字符串[]一切正常,但如果它是列表,则我的列表框中不会显示任何项目。我假设这是因为WPF不知道如何将Uri转换为字符串,但是 我想它只需要调用ToString(),这正是我想要的 我不知道如何告诉WPF如何做正确的事情 这是我的XAML: <ListBox Height="200" ItemsSource="{Binding Path=UrlsFound, Mode=OneWay}"> <ListBox.
字符串[]
一切正常,但如果它是列表
,则我的列表框
中不会显示任何项目。我假设这是因为WPF不知道如何将Uri
转换为字符串
,但是
ToString()
,这正是我想要的<ListBox Height="200" ItemsSource="{Binding Path=UrlsFound, Mode=OneWay}">
<ListBox.ItemTemplate>
<DataTemplate DataType="String">
<TextBlock Text="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
现在只要
UrlsFound
是一个string[]
绑定就可以工作,但是如果我重构使其成为一个列表
列表框中就不会显示任何内容。我将DataType=“String”
更改为“Uri”
,但这没有帮助因为我复制了您的XAML,所以肯定有其他问题
下面是我的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
urlsFound.Add(new Uri("http://www.google.com"));
urlsFound.Add(new Uri("http://www.google.com"));
urlsFound.Add(new Uri("http://www.google.com"));
this.DataContext = this;
}
private List<Uri> urlsFound=new List<Uri>();
public List<Uri> UrlsFound
{
get { return urlsFound; }
set { urlsFound = value; }
}
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
urlsFound.Add(新Uri(“http://www.google.com"));
urlsFound.Add(新Uri(“http://www.google.com"));
urlsFound.Add(新Uri(“http://www.google.com"));
this.DataContext=this;
}
私有列表urlsFound=新列表();
公开名单UrlsFound
{
获取{return urlsFound;}
设置{urlsFound=value;}
}
}
如何将数据类型
全部删除?我认为这与我的XAML主题有关。