简体   繁体   中英

Windows phone xaml data binding

I have a class Device which has an array of 4 sockets. each socket has id.

class Device
{   
    public String name { get; set; }        
    public Socket[] sArray = new Socket[4];        
}

class socket
{   
    public string id{ get; set; }
}

I want to print that all 4 id into listview's one item. each item has 5 textblocks. I can print device name using but how to print socket id from array of socket in device?

<TextBlock Name="txtSocket1" Text="how to bind here socket id?"/>
<TextBlock Name="txtSocket2" Text="how to bind here socket id?"/>
<TextBlock Name="txtSocket3" Text="how to bind here socket id?"/>
<TextBlock Name="txtSocket4" Text="how to bind here socket id?"/>
<TextBlock FontSize="28" Text="{Binding Path=name}"/> //here easily I can get device name

If you're showing a collection of items you shouldn't be using different textboxes. You want to use a control that is designed to show collections, like the ItemsControl :

<ItemsControl>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <TextBlock />
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

When using this approach, and WPF/SilverLight in general, I recommend using the MVVM pattern . Here you provide a ViewModel with data to be shown in the View:

public class SocketViewModel
{
    public string Name { get; set; }
    public List<Socket> Sockets { get; set; }

    public SocketViewModel()
    {
        Sockets = new List<Socket>();
    }
}

You fill up your data like this:

var viewModel = new SocketViewModel
{
    Name = "Some name";
    //This is some dummy just for demo purposes
    Sockets = YourDataBase.GetSockets();
}

Now you assign the viewmodel to the DataContext of the page, like this:

DataContext = viewModel;

In the XAML you can fetch all the data and show it on the screen. The correct XAML now should be something like this:

<ItemsControl ItemsSource="{Binding=Sockets}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Path=Id}" />
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

<TextBlock FontSize="28" Text="{Binding Path=Name}"/>

Note you should be binding to properties not fields. Create a property Public IEnumerable SocketsToBindTo { get {return _socketArray;}}

Then in each of text box you can bind to the property, create a converter and pass the array location number as a converter. In the converter you can use linq in the converter to do something like SocketsToBindTo.Skip(commandParameter).First() and return the name.

While I have told you the steps to get what you want. I don't think what you are doing is the right approach. If you are using a listview I would think you would just bind to the itemsource and then you just have to deal with setting your bindings on an Items template etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM