こんにちは。ご協力ありがとうございます。リストボックスに表示したいプロパティを持つオブジェクトのリストでデータバインディングを使用すると問題が発生します。
Silverlight の子ウィンドウに、xaml が次のようなリスト ボックスがあります。
<StackPanel Orientation="Vertical" Width="235">
<sdk:Label Content="Servers" HorizontalAlignment="Center"></sdk:Label>
<!--new group servers list box-->
<ListBox x:Name="NewGroupServersLB" Height="150" Width="200" ItemsSource="{Binding}" SelectionChanged="NewGroupServersLB_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ServerName}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
このコードで Binding オブジェクト インスタンスを使用して、コード ビハインドで DataContext を設定しています。
/*create new binding source object */
Binding serversBinding = new Binding();
/*set data source */
serversBinding.Source = childServersList;
/*set the data source for the new group servers listbox*/
NewGroupServersLB.DataContext = serversBinding;
デバッグを使用して、Binding オブジェクトのソースに 2 つのメンバーがあり、それらが childServersList からの適切なサーバー オブジェクト インスタンスであることを確認できます。ただし、実行するとリストボックスには何もありません。ただし、リストボックスのデータコンテキストをコードビハインドのサーバーリストに直接設定すると、サーバー名がリストボックスに表示されます。それがなぜなのか、または私が何か間違ったことをしているのか教えてください。Binding オブジェクトを使用しようとした理由は、最初のリスト ボックスから選択したサーバー オブジェクトの子メンバーを別のリスト ボックスに入力したいからです。助けてくれてありがとう、そしてこの質問の冗長さをお詫びします。
参考までに、リストに格納されているサーバー オブジェクトは次のようになります。
[XmlRoot("Server")]
public class RegisterServerObject
{
public RegisterServerObject() { }
[XmlElement("ServerID")]
[Browsable(false)]//not displayed in grids
[EditorBrowsable(EditorBrowsableState.Never)]//not displayed by intellisense
public string ServerIDString
{
set
{
ServerID = Convert.ToInt32(value);//convert the value passed in by xml to your type
}
get
{
return Convert.ToString(ServerID);
}
}
[XmlIgnore]
public int ServerID { get; set; }
[XmlElement("GroupID")]
public string GroupIDString
{
get
{
return this.GroupID.ToString();
}
set
{
if (string.IsNullOrEmpty(value))
{
this.GroupID = 0;
}
else
{
this.GroupID = int.Parse(value);
}
}
}
[XmlIgnore]
public int GroupID { get; set; }
[XmlElement("ParentID")]
public int ParentID { get; set; }
[XmlElement("ServerName")]
public string ServerName { get; set; }
[XmlElement("User")]
public string User { get; set; }
[XmlElement("UID")]
public int Uid { get; set; }
[XmlElement("PWD")]
public string Domain { get; set; }
[XmlElement("Location")]
public string Location { get; set; }
[XmlArray(ElementName = "AssociatedModules")]
[XmlArrayItem(ElementName = "Module")]
public List<RegisterModuleObject> AssociatedModules { get; set; }