开发者

How to check against Content name of the ListBoxItem?

I was able to toggle visibility of menuItem based on the property of another content (data in the datagrid). I need to be able to achieve the same with listbox items. I have hard time doing so. I am wondering if someone can help with that: Any help is highly appreciated!

The way it worked with menuItems: XAML:

<SplitButton:MenuButton x:Name="test" Content="Test">
 <SplitButton:MenuButton.ButtonMenuItemsSource>
      <toolkit:MenuItem x:Name="item1" Header="Item1" />
      <toolkit:MenuItem x:Name="item1" Header="Item2" />
  </SplitButton:MenuButton.ButtonMenuItemsSource>

I have problem with setting the same var for lisboxitem listbox. It is working perfect with MenuItems.

var item1Task = test.ButtonMenuItemsSource.OfType<ListBoxItem>().Where(temp => temp.Name == "item1").First();

I need to convert this coede line of setting var in a way that it will work with ListBoxItem:

Listbox XAML:

<toolkit:Expander x:Name="test" Header="Test">
<Border x:Name="Border">
  <ListBox x:Name="List">
         <ListBoxItem x:Name="item1" Content="Item1" />
         <ListBoxItem x:Name="item2" Content="Item2" />
   </ListBox>

I cannot use 开发者_运维问答OfType within listbox content. I tried something like that:

var item1Task = List.OfType<ListBoxItem>().Where(temp => temp.Content == "item1").First(); 

I hope it is possible. It is working great with the previous control, but I need to do the same using listbox. Thank you in advance for the help.


Do you need to find ListBoxItem named "item1" or which has "item1" as Content? To do the first, you may try this query:

var item1Task = List.Items.Cast<ListBoxItem>().First(temp => temp.Name == "item1");

If you need to find content, use this:

var item1Task = List.Items.Cast<ListBoxItem>().First(temp => temp.Content == "Item1");

You should give more attention to case sensitivity when comparing strings. Also there is no need to use Where and First together, because First already has conditional version.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜