使用 MVVM 将 TextBlock 绑定到 silveright 中的 listBox Item
我正在开发一个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>
我现在有两个问题。
我试图将列表框绑定到数据,这是一个字符串列表。我想要将每个元素放在单独的文本块中...我必须将此文本块绑定到哪个属性,以便它显示正确的数据
<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...
I am developing a silverlight navigation application and have encountered the following problem. I am using MVVM to connect a listbox with a class. The class hase a name, some text and a email address.
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;
}
}
The xaml file that contains the problematic part looks like this
<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>
I have now two questions.
I am trying to bind the listbox to Data which is a list of string. Each of this elements i want in a separated textblock... To which property do I have to bind this texblock so that it shows the right data
<ListBox x:Name="dataListBox" ItemsSource="{Binding Data, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="???"/> <!--What to put here???-->
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How can i make Hyperlink buttons clickable. I have set up all in the viewmodel but after i click the link nothing happens. I guess it's because the button is a list item but am not sure how to solve it.
Hope that anyone can help me with at least one problem...
Edit:
Thanks for the answers... the first one works great but the second doesnt... i have just the same commanding as on the site you mentiond. Here is what I did but it's not working:
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));
}
}
this is the actual url class just to have all clear
public class Url
{
public string Address { get; set; }
public string Action { get; set; }
public Url(string address, string action)
{
Address = address;
Action = action;
}
}
and the binding looks like this now
<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>
It isnt eveing firing the NavigateCommand in debug mode...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个新类而不是联系信息中的字符串列表
将字符串属性列表更改为 DataClass 列表
}
现在您可以将 Textblock 与 Dataclass 中名为“DataName”的属性绑定
create a new class instead of list of strings in contact info
change the list of string property to list of DataClass
}
now you can bind the Textblock with a property from Dataclass called "DataName"
1) Text="{绑定}"/>
2)使用属性创建新类型。String DisplayAddress,String Address,ICommand NavigateCommand; ,请参阅此链接以获取创建命令
http://www.silverlightshow.net/items/Silverlight -4-如何使用Command-Control.aspx
1) Text="{Binding}"/>
2) create new type with properties.String DisplayAddress ,String Address, ICommand NavigateCommand; , please see this link for craeting command
http://www.silverlightshow.net/items/Silverlight-4-How-to-Command-Control.aspx