我有一个包含一个列表框和一个 ContentControl 的 MainWindow,每次您从列表框中选择某些内容时,ContentControl 都会显示其他内容。
<ContentControl Content="{Binding ElementName=SomeList, Path=SelectedItem.Content}" />
<ListBox x:Name="SomeList" Margin="0 16 0 16" SelectedIndex="0" SelectedValue="{Binding X}"
ItemsSource="{Binding DemoItems}">
视图模型:
private string _X;
public string X
{
get { return _X; }
set
{
_X = value;
NotifyOfPropertyChange("X");
}
}
尝试显示 X 将导致相同的结果:
命名空间.DemoItem
DemoItem.cs:
public class DemoItem : INotifyPropertyChanged
{
private object _icon;
private string _name;
private object _content;
private Thickness _marginRequirement;
public DemoItem(object icon, string name, object content, Thickness margin, IEnumerable<DocumentationLink> documentation)
{
_icon = icon;
_name = name;
Content = content;
_marginRequirement = margin;
Documentation = documentation;
}
}
那么怎么可能只得到名字呢?