簡體   English   中英

用戶通過按下按鈕清除組合框

[英]User Clearing a ComboBox by Pressing a Button

我在 WPF 視圖上有一個 ComboBox,我希望用戶能夠通過按表單上的其他按鈕來清除所選項目。

這是組合框:

  <ComboBox Name="Relationship" FontWeight="Bold"
        DisplayMemberPath="Description" SelectedValuePath="Code"
        SelectedValue="{Binding Path=Form104CModel.SelectedRelationship, Mode=TwoWay}"
        >
        <ComboBox.ItemsSource>
            <CompositeCollection>
                <domain:Enumeration Code="" Description="" />
                <CollectionContainer Collection="{Binding Source={x:Static infraData:CodeCache.VictimSuspectRelationship}}" />
            </CompositeCollection>
        </ComboBox.ItemsSource>
    </ComboBox>

這是模型中的代碼:

//this.AddOtherVictimCommand = new DelegateCommand(OnAddVictimExecuted);

private void OnDeleteVictimExecuted(object InvNum)
{
    //  This method is called to set the default relationship for victim-to-suspect.  
    //  It is called when either the SelectedVictim or SelectedSuspect properties are set.  
    //  It searches the relationships collection by victim involvement number and suspect 
    //      involvement number. 
    //  When a match is found on both involvement numbers, it sets the SelectedRelationship 
    //      property to the relationship in the relationships collection.  
    //   If no match is found, nothing happens.
    // 
    int a = this.Relationships.Count;
    this.SelectedRelationship = null;
    //if ((this.SelectedSuspect != null) & (this.SelectedVictim != null))
    //{
    //    for (int i = 0; i < this.Relationships.Count; i++)
    //    {
    //        if (this.Relationships[i].SuspectNumber == this.SelectedSuspect.InvolvementNumber)
    //            if (this.Relationships[i].VictimNumber == this.SelectedVictim.InvolvementNumber)
    //                this.SelectedRelationship = null;
    //    }
    //}
}

從代碼中可以看出,我嘗試了一些無效的方法。 看起來應該很簡單,但我錯過了一些東西。

很簡單,你必須在你的空白處添加這一行

Relationship.Items.Clear();

最終代碼:

private void OnDeleteVictimExecuted(object InvNum)
    {
        Relationship.Items.Clear();
        //
        //  This method is called to set the default relationship for victim-to-suspect.  
        //  It is called when either the SelectedVictim or SelectedSuspect properties are set.  
        //  It searches the relationships collection by victim involvement number and suspect 
        //      involvement number. 
        //  When a match is found on both involvement numbers, it sets the SelectedRelationship 
        //      property to the relationship in the relationships collection.  
        //   If no match is found, nothing happens.
        // 
        int a = this.Relationships.Count;
        this.SelectedRelationship = null;
        //if ((this.SelectedSuspect != null) & (this.SelectedVictim != null))
        //{
        //    for (int i = 0; i < this.Relationships.Count; i++)
        //    {
        //        if (this.Relationships[i].SuspectNumber == this.SelectedSuspect.InvolvementNumber)
        //            if (this.Relationships[i].VictimNumber == this.SelectedVictim.InvolvementNumber)
        //                this.SelectedRelationship = null;
        //    }
        //}
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM