簡體   English   中英

綁定但不顯示下拉菜單項

[英]bind but not display items in drop down in c#

我在這里有一個問題。

我想將C#中的下拉列表與以下值綁定

value    text
-----    ----
1         abc
2         pqr
3         xyz
4         ppp

但是我只想顯示與1,2和3有關的項目。

是否可以綁定所有值,但是不顯示所有項目

您可以嘗試為最后一個ListItem對象設置Enabled = false 我的建議可能是不要將ListItem也包括在要綁定的項目列表中。

嘗試這個

list.DataSource = myDataSource;
list.DataBind();

list.Items.Remove(list.Items.FindByText("ppp"));

要么

list.Items.Remove(list.Items.FindByValue("4"));

您可以從DropDownList中刪除

ListItem itemToRemove = myDropDown.Items.FindByValue("4");
if (itemToRemove != null)
{
    myDropDown.Items.Remove(itemToRemove);
}

是的你可以。

在您的DataTemplate ,只需將對象的可見性綁定到...您的條件(似乎是索引?),並將“顯示/隱藏”邏輯包含在轉換器中。

編輯這樣的事情:

<DataTemplate>
  <ContentPresenter Visibility="{Binding Index, Converter={StaticRessource IndexTovisibilityConverter}}>
    // Here your datatemplate
  </ContentPresenter>
<Datatemplate>

在轉換器內部:

public class IndexTovisibilityConverter: IValueConverter
{

   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {
     int index= (int)value;

      if (index > 3)
        return Visibility.Collapsed;
      else
        return Visibility.Visible;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    return null;
  }
}

您可以在后面的代碼中做到這一點。

  ListItem l = new ListItem(); l.Text = "New"; l.Value = "new"; l.Attributes.CssStyle.Add("visibility", "hidden"); 

暫無
暫無

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

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