开发者

Binding TextBlock to listBox Item in silveright using MVVM

开发者 https://www.devze.com 2023-02-24 04:02 出处:网络
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

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 her开发者_如何学Pythone???-->
                                </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...


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消