Structure Link

I am trying to bind a listview element to a member of a structure, but I cannot get it to work.

The structure is pretty simple:

public struct DeviceTypeInfo
{
    public String deviceName;
    public int deviceReferenceID;
};

in my view model I keep a list of these structures and I want the "deviceName" to appear in the list.

public class DevicesListViewModel
{
    public DevicesListViewModel( )
    {

    }

    public void setListOfAvailableDevices(List<DeviceTypeInfo> devicesList)
    {
        m_availableDevices = devicesList;
    }

    public List<DeviceTypeInfo> Devices
    {
        get { return m_availableDevices; }
    }

    private List<DeviceTypeInfo> m_availableDevices;
}

I tried the following, but I can not get the binding to work, do I need to use a native source?

    <ListBox Name="DevicesListView" Grid.Column="0" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="10"  MinHeight="250" MinWidth="150"  ItemsSource="{Binding Devices}" Width="Auto">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding DeviceTypeInfo.deviceName}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
+5
source share
3 answers

You need to make members in the structure properties.

public struct DeviceTypeInfo 
{    
    public String deviceName { get; set; }     
    public int deviceReferenceID { get; set; } 
}; 

Yesterday I faced a similar situation: P

EDIT: Oh yes, and as Jesse said, after you include them in the properties, you'll want to set up the INotifyPropertyChanged event .

+9
source

TextBlock DataContext DeviceTypeInfo, deviceName, DeviceTypeInfo.deviceName.

<DataTemplate>
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding deviceName}"/>
    </StackPanel>
</DataTemplate>

, Properties, . , { get; set; } , answer.google.com

+4

I think you need getters and setters. You may also need to implement INotifyPropertyChanged.

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
+3
source

All Articles