[英]Binding list to listbox and show more info of currently selected item
我有一個班級敵人
public class Enemy : Entity
{
private int _BaseAddress;
private int _BaseOffset;
private int _HealthOffset;
private int _AmmoOffset;
private int _X_Coord_Offset;
private int _Y_Coord_Offset;
private int _Z_Coord_Offset;
public Enemy(int BaseAddress, int BaseOffset, int HealthOffset, int AmmmoOffset, int X_Coord_Offset, int Y_Coord_Offset, int Z_Coord_Offset)
{
_BaseAddress = BaseAddress;
_BaseOffset = BaseOffset;
_HealthOffset = HealthOffset;
_AmmoOffset = AmmmoOffset;
_X_Coord_Offset = X_Coord_Offset;
_Y_Coord_Offset = Y_Coord_Offset;
_Z_Coord_Offset = Z_Coord_Offset;
}
public int BaseAddress
{
get
{
int temp = BitConverter.ToInt32(ReadMemory(_BaseAddress, sizeof(int)), 0);
return BitConverter.ToInt32(ReadMemory(temp + _BaseOffset, sizeof(int)), 0);
}
}
public int Health
{
get
{
return BitConverter.ToInt32(ReadMemory(BaseAddress + _HealthOffset, sizeof(int)), 0);
}
}
public int Ammo
{
get
{
return BitConverter.ToInt32(ReadMemory(BaseAddress + _AmmoOffset, sizeof(int)), 0);
}
}
public float X_Coordinate
{
get
{
return BitConverter.ToSingle(ReadMemory(BaseAddress + _X_Coord_Offset, sizeof(float)), 0);
}
}
public float Y_Coordinate
{
get
{
return BitConverter.ToSingle(ReadMemory(BaseAddress + _Y_Coord_Offset, sizeof(float)), 0);
}
}
public float Z_Coordinate
{
get
{
return BitConverter.ToSingle(ReadMemory(BaseAddress + _Z_Coord_Offset, sizeof(float)), 0);
}
}
//I know this has no value
public string name;
}
我有一個敵人列表
List<Enemy> list = new List<Enemy>();
我有一個列表框
<ListBox Grid.Column="1" Grid.Row="1" Name="lstBEnemies" Margin="20"/>
我有一些標簽。 我希望對於列表中的每個敵人對象,都會創建一個 listboxitem 並且內容是字符串名稱。 此外,當我選擇一個列表框項目時,我希望它通過標簽向我顯示所選敵人對象的所有信息。
我已經設法將列表的每個項目添加到列表框
for (int i = 0; i < EnemyList.Count; i++)
{
lstBEnemies.Items.Add(EnemyList[i]);
}
我還創建了一個計時器,每 5 毫秒更新一次標簽上的信息。 我的問題是,我該怎么做才能通過標簽顯示當前選定項目的敵人對象的信息?
編輯:這就是我更新標簽的方式
private void DpTimerTick_GetEnemyAmmo(object sender, EventArgs e)
{
lbEnemyAmmoInfo.Content = EnemyList[0].Ammo;
}
您可以將TextBlock
的Text
屬性綁定到ListBox
的SelectedItem
:
<TextBlock Text="{Binding SelectedItem.BaseAddress, ElementName=lstBEnemies}" />
標簽的相應綁定是:
<Label Content="{Binding SelectedItem.BaseAddress, ElementName=lstBEnemies}" />
編輯:
那么為什么不簡單地選擇合適的項目呢?:
private void DpTimerTick_GetEnemyAmmo(object sender, EventArgs e)
{
lstBEnemies.SelectedItem = EnemyList[0];
}
<Label Content="{Binding SelectedItem.Ammo, ElementName=lstBEnemies}" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.