WPF 将 ObservableCollection<string> 绑定到 ItemsControl 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12317682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF binding ObservableCollection<string> to ItemsControl not working
提问by karambolis8
I have some string property that content need to be parsed, and displayed in separate controls. Let's say there are e-mails and urls separated by whitespaces. Displaying emails works fine, but urls don't. Here's code:
我有一些需要解析内容并显示在单独控件中的字符串属性。假设有由空格分隔的电子邮件和网址。显示电子邮件工作正常,但网址不行。这是代码:
XAML file:
XAML 文件:
<ScrollViewer Grid.Row="0" Grid.Column="5" >
<StackPanel Orientation="Vertical">
<TextBox Name="EmailTextBox"
Text="{Binding ElementName=candidatePersonalDataViewControl, Mode=OneWay, Path=Candidate.Email, Converter={StaticResource emailConverter}}"/>
<ItemsControl ItemsSource="{Binding ElementName=candidatePersonalDataViewControl, Mode=OneWay, Path=UrlsProperty}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock
Margin="0,0,0,0"
Padding="5,2,5,2"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Hyperlink NavigateUri="{Binding Path=/}"
Click="EmailAsWWW_Click">
<Run Text="{Binding Path=/}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</ScrollViewer>
C# file:
C#文件:
private static readonly DependencyProperty CandidateProperty = DependencyProperty.Register("Candidate", typeof(Candidate), typeof(CandidatePersonalDataViewControl));
public Candidate Candidate
{
get { return (Candidate)GetValue(CandidateProperty); }
set { SetValue(CandidateProperty, value); }
}
public CandidatePersonalDataViewControl()
{
InitializeComponent();
this.DataContext = this;
UrlsProperty = new ObservableCollection<string>();
}
public ObservableCollection<string> UrlsProperty
{
get;
private set;
}
private void Grid_LayoutUpdated(object sender, EventArgs e)
{
if (Candidate == null)
return;
if (Candidate.Email == null)
return;
EmailPropertyToEmailsConverter emailConverter = new EmailPropertyToEmailsConverter();
bool emails = (emailConverter.Convert(Candidate.Email, typeof(string), null, null) as string) != null;
this.EmailTextBox.Visibility = emails ? Visibility.Visible : Visibility.Collapsed;
EmailPropertyToUrlListConverter urlConverter = new EmailPropertyToUrlListConverter();
IList<string> urls = urlConverter.Convert(Candidate.Email, typeof(string), null, null) as IList<string>;
UrlsProperty.Clear();
if (urls != null)
foreach (string url in urls)
UrlsProperty.Add(url);
}
I have debugged Grid_LayoutUpdated and it works fine. It's fired whenever it's needed and UrlsCollection contains desired data. As I said, EmailTextBox displays the data, but ItemsControl is empty all the time. What is more interesing, removing "Path=/" from Hyperlink bindings raises an XAML Parse exception. I've run out of ideas... I have read some topics, but none was helpful
我已经调试了 Grid_LayoutUpdated 并且它工作正常。它在需要时被触发,并且 UrlsCollection 包含所需的数据。正如我所说,EmailTextBox 显示数据,但 ItemsControl 一直为空。更有趣的是,从超链接绑定中删除“Path=/”会引发 XAML 解析异常。我的想法已经用完了...我已经阅读了一些主题,但没有一个有帮助
wpf ItemsControl binding problem
WPF Binding Path=/ not working?
WPF - bind a listbox to a list<string> - what am I doing wrong?
WPF - 将列表框绑定到 list<string> - 我做错了什么?
回答by H.B.
Path=/binds to the current item of a collectionnot the current item that is being templated, usually said current item is the one that is selected (see IsSynchronizedWithCurrentItem). Your DataContextis a string and not a collection, hence trying to bind to a current item via /will fail (see binding errors).
Path=/结合到当前项的集合不是正被模板化当前项目,通常所说的当前项是被选择(见一个IsSynchronizedWithCurrentItem)。您DataContext是一个字符串而不是一个集合,因此尝试绑定到当前项目 via/将失败(请参阅绑定错误)。
To bind to the current item that is being templatedyou need to bind directly to the DataContextwhich can be done via {Binding}or {Binding .}/{Binding Path=.}(if the property requires a Binding.Pathchoose one of the latter).
要绑定到正在模板化的当前项目,您需要直接绑定到DataContext可以通过{Binding}或{Binding .}/完成的项目{Binding Path=.}(如果该属性需要Binding.Path选择后者之一)。
回答by blindmeis
your hyperlink control need an Uri for the NavigateUri property, but you just bind to a string.
您的超链接控件需要 NavigateUri 属性的 Uri,但您只需绑定到一个字符串。
one way to achieve what you want is using a converter for your NavigateUri Binding
实现您想要的一种方法是为您的 NavigateUri 绑定使用转换器
<Hyperlink NavigateUri="{Binding Path=., Converter={StaticResource YourStringToUriConverterGoesHere}}" Click="EmailAsWWW_Click">
<Run Text="{Binding}" />
</Hyperlink>
an other way is change the collection
另一种方法是更改集合
public ObservableCollection<MyUrlWrapper> UrlsProperty
{
get;
private set;
}
public class MyUrlWrapper
{
public string Url {get;set;}
public Uri MyUri {get{return new Uri(this.Url);}}
}
<Hyperlink NavigateUri="{Binding Path=MyUri}" Click="EmailAsWWW_Click">
<Run Text="{Binding Path=Url}" />
</Hyperlink>
回答by GazTheDestroyer
Try:
尝试:
<Hyperlink NavigateUri="{Binding}" Click="EmailAsWWW_Click">
<Run Text="{Binding}" />
</Hyperlink>

