我正在开发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>

我现在有两个问题。

我正在尝试将列表框绑定(bind)到作为字符串列表的数据。我想在单独的文本块中使用每个元素...我必须将此属性绑定(bind)到哪个属性,以便它显示正确的数据
<ListBox x:Name="dataListBox"  ItemsSource="{Binding Data, Mode=TwoWay}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="???"/> <!--What to put here???-->
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>

如何使超链接按钮可单击。我已经在viewmodel中设置了所有内容,但是单击链接后没有任何 react 。我猜这是因为按钮是一个列表项,但不确定如何解决。

希望任何人都能帮助我解决至少一个问题...

编辑:
感谢您的回答...第一个效果很好,但第二个效果不...我的命令与您提到的站点相同。这是我所做的,但无法正常工作:
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;
    }
}

现在绑定(bind)看起来像这样
<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>

它不是在 Debug模式下触发NavigateCommand ...

最佳答案

1)Text =“{Binding}”/>
2)使用properties.String DisplayAddress,String Address,ICommand NavigateCommand创建新类型; ,请查看此链接以获取craeting命令
http://www.silverlightshow.net/items/Silverlight-4-How-to-Command-Control.aspx

关于silverlight - 使用MVVM将TextBlock绑定(bind)到Silveright中的listBox项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5613648/

10-13 06:52