2

我有一些字符串属性需要解析内容,并显示在单独的控件中。假设有电子邮件和网址用空格分隔。显示电子邮件工作正常,但网址却不行。这是代码:

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# 文件:

    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);
    }

我已经调试了 Grid_LayoutUpdated 并且工作正常。它会在需要时触发,并且 UrlsCollection 包含所需的数据。正如我所说,EmailTextBox 显示数据,但 ItemsControl 一直是空的。更有趣的是,从超链接绑定中删除“Path=/”会引发 XAML Parse 异常。我的想法已经用完了......我已经阅读了一些主题,但没有一个有用

wpf ItemsControl绑定问题

WPF绑定路径=/不工作?

WPF - 将列表框绑定到 list<string> - 我做错了什么?

如何将 ObservableCollections 绑定到 ItemsSource?

4

3 回答 3

3

Path=/绑定到集合的当前而不是被模板化的当前项,通常所说的当前项是被选中的项(请参阅IsSynchronizedWithCurrentItem)。你DataContext是一个字符串而不是一个集合,因此尝试通过绑定到当前项目/将失败(请参阅绑定错误)。

要绑定到正在模板化的当前项目,您需要直接绑定到DataContext可以通过{Binding}{Binding .}/完成的项目{Binding Path=.}(如果属性需要Binding.Path选择后者之一)。

于 2012-09-07T13:33:42.047 回答
1

您的超链接控件需要一个用于 NavigateUri 属性的 Uri,但您只需绑定到一个字符串。

实现您想要的一种方法是为您的 NavigateUri 绑定使用转换器

<Hyperlink NavigateUri="{Binding Path=., Converter={StaticResource YourStringToUriConverterGoesHere}}" Click="EmailAsWWW_Click">
  <Run Text="{Binding}" />
</Hyperlink>

另一种方法是更改​​集合

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>
于 2012-09-07T12:33:39.647 回答
0

尝试:

<Hyperlink NavigateUri="{Binding}" Click="EmailAsWWW_Click">
    <Run Text="{Binding}" />
</Hyperlink>
于 2012-09-07T12:10:31.827 回答