【发布时间】:2011-08-02 14:06:53
【问题描述】:
我正在开发一个 Silverlight 导航应用程序,遇到了以下问题。我正在使用 MVVM 将列表框与类连接起来。这个类有一个名字、一些文本和一个电子邮件地址。
public class ContactPage
{
public List<ContactInfo> Contacts { get; set; }
public Image Image { get; set; }
public string Description { get; set; }
//some other code not needed
}
public class ContactInfo
{
public string Name { get; set; }
public List<string> Data { get; set; }
public List<Url> Urls { get; set; }
public ContactInfo(string name, List<string> data, List<string> urls )
{
Name = name;
Data = data;
Urls = urls;
}
}
包含问题部分的 xaml 文件如下所示
<ListBox ItemsSource="{Binding ContactPage.Contacts, Mode=TwoWay}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name, Mode=TwoWay}" FontWeight="Bold"/>
<ListBox x:Name="dataListBox" ItemsSource="{Binding Data, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="???"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox ItemsSource="{Binding Urls, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<HyperlinkButton Content="{Binding Address, Mode=TwoWay}" ClickMode="Press">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding NavigateCommand}"
CommandParameter="{Binding Action, Mode=TwoWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</HyperlinkButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我现在有两个问题。
我正在尝试将列表框绑定到作为字符串列表的数据。我想要在一个单独的文本块中的每个元素......我必须将此 texblock 绑定到哪个属性,以便它显示正确的数据
<ListBox x:Name="dataListBox" ItemsSource="{Binding Data, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="???"/> <!--What to put here???-->
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如何使超链接按钮可点击。我已经在视图模型中设置了所有内容,但是单击链接后没有任何反应。我猜是因为按钮是一个列表项,但不知道如何解决。
希望任何人都可以帮助我解决至少一个问题...
编辑: 感谢您的回答...第一个效果很好,但第二个没有...我的命令与您提到的网站上的命令相同。这是我所做的,但它不起作用:
public ICommand NavigateCommand
{
get { return new RelayCommand<object>(param => Navigate(param), param => true); }
}
private void Navigate (object parameter)
{
Url url = parameter as Url;
if (url.Action.StartsWith("http"))
{
HtmlPage.Window.Navigate(new Uri(url.Action, UriKind.Absolute), "_blank");
}
else if (url.Action.StartsWith("mailto"))
{
HtmlPage.Window.Navigate(new Uri(url.Action, UriKind.Absolute));
}
}
这是实际的 url 类,只是为了让所有内容都清楚
public class Url
{
public string Address { get; set; }
public string Action { get; set; }
public Url(string address, string action)
{
Address = address;
Action = action;
}
}
现在绑定看起来像这样
<ListBox Name="linkListBox" ItemsSource="{Binding Urls, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<HyperlinkButton Content="{Binding Address, Mode=TwoWay}" ClickMode="Press">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding NavigateCommand}"
CommandParameter="{Binding ElementName=linkListBox, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</HyperlinkButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
它不会在调试模式下触发 NavigateCommand...
【问题讨论】:
标签: silverlight binding mvvm hyperlink listbox