gpt4 book ai didi

c# - WPF MVVM 层次结构选择项

转载 作者:太空狗 更新时间:2023-10-30 00:55:23 24 4
gpt4 key购买 nike

我目前正在使用ListBoxes 实现显示hierarchy 的应用程序(请不要建议使用TreeViewListBoxes 是必需的)。

看起来像那样in the article: WPF’s CollectionViewSource (with source code) .

enter image description here

类:

public class Mountains : ObservableCollection<Mountain>
{
public ObservableCollection<Lift> Lifts { get; }

public string Name { get; }
}

public class Lift
{
public ObservableCollection<string> Runs { get; }
}

该示例使用 CollectionViewSource 实例(参见 XAML)来简化设计。Mountains 类的实例是窗口的 DataContext


问题是:我希望 Mountains 类具有 SelectedRun 属性,并且它应该设置为当前选择的运行。

public class Mountains : ObservableCollection<Mountain>
{
public ObservableCollection<Lift> Lifts { get; }

public string Name { get; }

public string SelectedRun { get; set; }
}

也许我错过了一些基本原则,但我怎样才能做到这一点?

最佳答案

您可能想阅读有关在绑定(bind)中使用“/”的信息。请参阅此 MSDN article 上的“当前项目指针”部分.

这是我的解决方案:

Xaml

    <Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>

<TextBlock Margin="5" Grid.Row="0" Grid.Column="0" Text="Mountains"/>
<TextBlock Margin="5" Grid.Row="0" Grid.Column="1" Text="Lifts"/>
<TextBlock Margin="5" Grid.Row="0" Grid.Column="2" Text="Runs"/>

<ListBox Grid.Row="1" Grid.Column="0" Margin="5"
ItemsSource="{Binding Mountains}" DisplayMemberPath="Name"
IsSynchronizedWithCurrentItem="True" />

<ListBox Grid.Row="1" Grid.Column="1" Margin="5"
ItemsSource="{Binding Mountains/Lifts}" DisplayMemberPath="Name"
IsSynchronizedWithCurrentItem="True"/>

<ListBox Grid.Row="1" Grid.Column="2" Margin="5"
ItemsSource="{Binding Mountains/Lifts/Runs}"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedRun}"/>
</Grid>

C#(注意,您不需要实现 INotifyPropertyChanged 除非属性将被更改而不仅仅是被选中)

public class MountainsViewModel
{
public MountainsViewModel()
{
Mountains = new ObservableCollection<Mountain>
{
new Mountain
{
Name = "Whistler",
Lifts = new ObservableCollection<Lift>
{
new Lift
{
Name = "Big Red",
Runs = new ObservableCollection<string>
{
"Headwall",
"Fisheye",
"Jimmy's"
}
},
new Lift
{
Name = "Garbanzo",
Runs = new ObservableCollection<string>
{
"Headwall1",
"Fisheye1",
"Jimmy's1"
}
},
new Lift {Name = "Orange"},
}

},
new Mountain
{
Name = "Stevens",
Lifts = new ObservableCollection<Lift>
{
new Lift {Name = "One"},
new Lift {Name = "Two"},
new Lift {Name = "Three"},
}

},
new Mountain {Name = "Crystal"},
};
}

public string Name { get; set; }
private string _selectedRun;
public string SelectedRun
{
get { return _selectedRun; }
set
{
Debug.WriteLine(value);
_selectedRun = value;
}
}

public ObservableCollection<Mountain> Mountains { get; set; }
}

public class Mountain
{
public string Name { get; set; }

public ObservableCollection<Lift> Lifts { get; set; }
}

public class Lift
{
public string Name { get; set; }

public ObservableCollection<string> Runs { get; set; }
}

关于c# - WPF MVVM 层次结构选择项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9827961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com