お世話になっております。
行いたいこと
ItemsControlに動的に追加されたアイテムの表示・非表示をコードビハインドで制御したいです。
PersonクラスにIsVisibleプロパティなどを追加してコンバーターを用いる方法なども考えたのですが、
できるだけPersonクラスにプロパティを追加や継承して新たなクラスを作成したくありません。
なにか良い方法はありますでしょうか。
よろしくお願いいたします。
コード
```csharp
public partial class Window1 : Window
{
public ObservableCollection<Person> Persons { get; set; } = new ObservableCollection<Person>()
{
new Person(){ Name = "taro", Age = 20 },
new Person(){ Name = "jiro", Age = 18 },
new Person(){ Name = "saburo", Age = 15 }
};
public Window1() { InitializeComponent(); this.DataContext = this; } private void Button_Click(object sender, RoutedEventArgs e) { // 0番目(taro)を非表示にしたい。 } } public class Person : BindableBase //BindableBaseはINotifyPropertyChangedを継承したもの { private string _name; private int _age: public string Index { get { return _name; } set { SetProperty(ref _name, value); } } public int Index { get { return _age; } set { SetProperty(ref _age, value); } } }
XAML ```XAML <Grid> <Button Content="test" Click="Button_Click"/> <ItemsControl ItemsSource="{Binding Persons}" Name="icPersons"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}"/> <TextBlock Text=":"/> <TextBlock Text="{Binding Age}"/> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Grid>
環境
C# .NET 4.6, VS2017 Pro
回答1件
あなたの回答
tips
プレビュー