结合到结构 [英] Binding to a structure
问题描述
我想一个ListView项绑定到一个结构中的一员,但我无法得到它的工作。
结构很简单:
公共结构DeviceTypeInfo
{
公共字符串DEVICENAME;
公众诠释deviceReferenceID;
};
在我的视图模型我认为这些结构的列表,我想要得到的设备名称,以显示在列表框中。
公共类DevicesListViewModel
{
公共DevicesListViewModel()
{ } 公共无效setListOfAvailableDevices(列表< DeviceTypeInfo> devicesList)
{
m_availableDevices = devicesList;
} 公开名单< DeviceTypeInfo>设备
{
{返回m_availableDevices; }
} 私人列表< DeviceTypeInfo> m_availableDevices;
}
我曾尝试以下,但我不能让绑定工作,做我需要使用的RelativeSource?
<列表框名称=DevicesListViewGrid.Column =0VerticalAlignment =底的HorizontalAlignment =中心保证金=10了minHeight =250了minWidth = 150的ItemsSource ={结合设备}WIDTH =自动>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
< StackPanel的方向=垂直>
< TextBlock的文本={结合DeviceTypeInfo.deviceName}/>
< / StackPanel的>
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
您需要让会员在结构中的属性。
公共结构DeviceTypeInfo
{
公共字符串DEVICENAME {搞定;组; }
公众诠释deviceReferenceID {搞定;组; }
};
昨天我碰到了类似的情况:P
编辑:哦,是的,而像杰西说,一旦你把它们变成属性,你要设置的<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx\">INotifyPropertyChanged事件。
I am trying to bind a listview item to a member of a structure, but I am unable to get it to work.
The structure is quite simple:
public struct DeviceTypeInfo
{
public String deviceName;
public int deviceReferenceID;
};
in my view model I hold a list of these structures and I want to get the "deviceName" to be displayed in a list box.
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 have tried the following but I can't get the binding to work, do I need to use relativesource?
<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>
You need to make the members in the struct properties.
public struct DeviceTypeInfo
{
public String deviceName { get; set; }
public int deviceReferenceID { get; set; }
};
I ran into a similar situation yesterday :P
EDIT: Oh yeah, and like Jesse said, once you turn them into properties, you'll want to set up the INotifyPropertyChanged event.
这篇关于结合到结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!