0

こんにちは。ご協力ありがとうございます。リストボックスに表示したいプロパティを持つオブジェクトのリストでデータバインディングを使用すると問題が発生します。

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; }
4

1 に答える 1

1

バインディング ソース (データ バインディング先のオブジェクト) を指定するには、いくつかの方法があります。1 つ目は、DataContext を指定することです。これは、XAML またはコードで実行できます。

NewGroupServersLB.DataContext = childServersList;

DataContext がソース オブジェクトに設定されていることに注意してください。

代わりに Binding オブジェクトを使用している場合は、Binding オブジェクトの Source をバインディング ソースに設定します。

MyData myDataObject = new MyData(DateTime.Now);      
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
myText.SetBinding(TextBlock.TextProperty, myBinding);

この場合、Binding オブジェクトを設定してから、それをコントロールに適用します。DataContext は設定されていませんが、設定されていた場合、Binding オブジェクトが優先されるため無視されます。

特定の例については、Data Binding ドキュメントの次の特定のセクションをお読みください。

http://msdn.microsoft.com/en-us/library/ms752347.aspx#master_detail_scenario

于 2012-08-27T17:42:00.263 に答える