Silverlight DataTemplate中的绑定问题
我在DataTemplate中进行数据绑定时遇到了一些问题。在网格中文本框下方的ControlTemplate中工作并打印出正确的值。但是HyperlinkButtons数据模板中的文本块不起作用。尽管HyperlinkButtons NavigateUri已正确绑定。有人能帮我吗 我创建了一个简单的例子来说明我的问题 MainPage.xamlSilverlight DataTemplate中的绑定问题,silverlight,Silverlight,我在DataTemplate中进行数据绑定时遇到了一些问题。在网格中文本框下方的ControlTemplate中工作并打印出正确的值。但是HyperlinkButtons数据模板中的文本块不起作用。尽管HyperlinkButtons NavigateUri已正确绑定。有人能帮我吗 我创建了一个简单的例子来说明我的问题 MainPage.xaml <UserControl x:Class="SilverlightApplication8.MainPage" xmlns="http:
<UserControl x:Class="SilverlightApplication8.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<ControlTemplate x:Key="EventControlTemplate" TargetType="Button">
<Grid>
<!--WORKS-->
<!--<Grid >
<TextBlock x:Name="TitleTextBlock" Text="{Binding Title}" Foreground="Red" FontWeight="Bold" />
</Grid>-->
<!--DOES NOT WORK-->
<HyperlinkButton TargetName="_blank" NavigateUri="{Binding Url}" >
<HyperlinkButton.ContentTemplate>
<DataTemplate>
<Grid>
<TextBlock Foreground="Green" Text="{Binding Title}"/>
</Grid>
</DataTemplate>
</HyperlinkButton.ContentTemplate>
</HyperlinkButton>
</Grid>
</ControlTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ItemsControl x:Name="Links" Foreground="White" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border >
<Button Template="{StaticResource EventControlTemplate}" Click="Button_Click"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
MainPage.xaml.cs
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace SilverlightApplication8
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
List<Events> events = new List<Events>();
events.Add(new Events(){Title = "This is title 1", Url = "http://www.thesun.co.uk"});
events.Add(new Events(){Title = "This is title 2", Url = "http://www.thesun.co.uk"});
events.Add(new Events() { Title = "This is title 3", Url = "http://www.thesun.co.uk" });
Links.ItemsSource = events;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
public class Events
{
public string Url { get; set; }
public string Title { get; set; }
}
}
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;
命名空间SilverlightApplication8
{
公共部分类主页面:UserControl
{
公共主页()
{
初始化组件();
列表事件=新列表();
添加(新事件(){Title=“这是标题1”,Url=”http://www.thesun.co.uk"});
添加(新事件(){Title=“这是标题2”,Url=”http://www.thesun.co.uk"});
添加(新事件(){Title=“这是标题3”,Url=”http://www.thesun.co.uk" });
Links.ItemsSource=事件;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
}
}
公开课活动
{
公共字符串Url{get;set;}
公共字符串标题{get;set;}
}
}
您正在设置内容模板
,其中的数据上下文
将是超链接按钮的内容
。由于您尚未设置内容
,因此您正在尝试针对null
进行绑定
这应该起作用:
<HyperlinkButton TargetName="_blank" NavigateUri="{Binding Url}" Content="{Binding Title}">
<HyperlinkButton.ContentTemplate>
<DataTemplate>
<Grid>
<TextBlock Foreground="Green" Text="{Binding .}"/>
</Grid>
</DataTemplate>
</HyperlinkButton.ContentTemplate>
</HyperlinkButton>
欢迎来到SO,请花几分钟时间阅读常见问题解答和降价文档(编辑问题时,右边空白处有一个有用的语法)。我简化了示例。但是我在网格中有多个绑定,但是我只是修改了Content=“{Binding}”,在DataTemplate中我可以在网格中添加许多textblock:)太棒了。非常感谢!!!
<HyperlinkButton TargetName="_blank" NavigateUri="{Binding Url}" Content="{Binding Title}"/>