使用 MVVM 将 TextBlock 绑定到 silveright 中的 listBox Item

发布于 2024-10-31 14:05:37 字数 5391 浏览 1 评论 0原文

我正在开发一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰葑 2024-11-07 14:05:37

创建一个新类而不是联系信息中的字符串列表

public DataClass
{
    public string DataName { get; set; }

    public DataClass(string dataName)
    {
        DataName = dataName;
    }
}

将字符串属性列表更改为 DataClass 列表

public class ContactInfo
{
   public string Name { get; set; }
   public List<Dataclass> Data { get; set; }
   public List<Url> Urls { get; set; }

   public ContactInfo(string name, List<string> data, List<string> urls )
   {
        Name = name;      

        Urls = urls;

    var objDataClass = ne List<Dataclass>();

    foreach(string str in data)
    {
        objDataClass.Add(new Dataclass(str));
    }
    Data = objDataClass;
}            

}

现在您可以将 Textblock 与 Dataclass 中名为“DataName”的属性绑定

 <ListBox x:Name="dataListBox"  ItemsSource="{Binding Data, Mode=TwoWay}">
    <ListBox.ItemTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding DataName}"/>
         </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

create a new class instead of list of strings in contact info

public DataClass
{
    public string DataName { get; set; }

    public DataClass(string dataName)
    {
        DataName = dataName;
    }
}

change the list of string property to list of DataClass

public class ContactInfo
{
   public string Name { get; set; }
   public List<Dataclass> Data { get; set; }
   public List<Url> Urls { get; set; }

   public ContactInfo(string name, List<string> data, List<string> urls )
   {
        Name = name;      

        Urls = urls;

    var objDataClass = ne List<Dataclass>();

    foreach(string str in data)
    {
        objDataClass.Add(new Dataclass(str));
    }
    Data = objDataClass;
}            

}

now you can bind the Textblock with a property from Dataclass called "DataName"

 <ListBox x:Name="dataListBox"  ItemsSource="{Binding Data, Mode=TwoWay}">
    <ListBox.ItemTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding DataName}"/>
         </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>
猛虎独行 2024-11-07 14:05:37

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文