列表框中的文本块未填充值
Textblock in Listbox not populating with values
我正在尝试在列表框中填充文本块,如下所示。
首先我尝试了这个 xaml,效果很好。
<ListBox Name="lbTodoList" HorizontalContentAlignment="Stretch"
ItemsSource="{Binding ThisList1}"
DisplayMemberPath="{Binding ListItem.Name}"
>
</ListBox>
然而,在下面尝试时,文本块中没有显示任何内容
<StackPanel Grid.Row="1">
<TextBox Text="{Binding ElementName=lbTodoList, Path=SelectedValue}"/>
<Button Command="{Binding ChangeCurrentView}">Click Me</Button>
<Button Command="{Binding ChangeCurrentView}">Click Me</Button>
<ListBox Name="lbTodoList" HorizontalContentAlignment="Stretch"
ItemsSource="{Binding ThisList1}"
Width="200"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Width="auto" Foreground="Black" Text="{Binding Path=ListItem.Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
无法弄清楚我在这里做错了什么。任何帮助表示赞赏。
您正在尝试显示对象 ListItem
的名为 ThisList1
的集合,您应该正确定义您的模型,在这里您应该做什么:
定义项目 class:
public class ListItem
{
public String Name { get; set; }
//add your properties
}
在您的代码隐藏或 Viewmodel 中定义将绑定到 ListBox
的集合
private ObservableCollection<ListItem> _thisList1=new ObservableCollection<ListItem>()
{
new ListItem()
{
Name = "Name1",
}, new ListItem()
{
Name = "Name2",
}
} ;
public ObservableCollection<ListItem> ThisList1
{
get
{
return _thisList1;
}
set
{
_thisList1 = value;
}
}
终于在这里如何定义ListBox
的DataTemplate
<StackPanel Grid.Row="1">
<TextBox Text="{Binding ElementName=lbTodoList, Path=SelectedItem.Name}"/>
<Button Command="{Binding ChangeCurrentView}">Click Me</Button>
<Button Command="{Binding ChangeCurrentView}">Click Me</Button>
<ListBox Name="lbTodoList" HorizontalContentAlignment="Stretch"
ItemsSource="{Binding ThisList1}"
Width="200"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Width="auto" Foreground="Black" Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
不要忘记使用代码隐藏设置 DataContext
:
this.DataContext=this;
或通过Xaml
DataContext="{Binding RelativeSource={RelativeSource Self}}"
并考虑实现 INotifypropertyChanged
接口以从视图模型自动更新 UI。
我正在尝试在列表框中填充文本块,如下所示。
首先我尝试了这个 xaml,效果很好。
<ListBox Name="lbTodoList" HorizontalContentAlignment="Stretch"
ItemsSource="{Binding ThisList1}"
DisplayMemberPath="{Binding ListItem.Name}"
>
</ListBox>
然而,在下面尝试时,文本块中没有显示任何内容
<StackPanel Grid.Row="1">
<TextBox Text="{Binding ElementName=lbTodoList, Path=SelectedValue}"/>
<Button Command="{Binding ChangeCurrentView}">Click Me</Button>
<Button Command="{Binding ChangeCurrentView}">Click Me</Button>
<ListBox Name="lbTodoList" HorizontalContentAlignment="Stretch"
ItemsSource="{Binding ThisList1}"
Width="200"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Width="auto" Foreground="Black" Text="{Binding Path=ListItem.Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
无法弄清楚我在这里做错了什么。任何帮助表示赞赏。
您正在尝试显示对象 ListItem
的名为 ThisList1
的集合,您应该正确定义您的模型,在这里您应该做什么:
定义项目 class:
public class ListItem
{
public String Name { get; set; }
//add your properties
}
在您的代码隐藏或 Viewmodel 中定义将绑定到 ListBox
private ObservableCollection<ListItem> _thisList1=new ObservableCollection<ListItem>()
{
new ListItem()
{
Name = "Name1",
}, new ListItem()
{
Name = "Name2",
}
} ;
public ObservableCollection<ListItem> ThisList1
{
get
{
return _thisList1;
}
set
{
_thisList1 = value;
}
}
终于在这里如何定义ListBox
的DataTemplate
<StackPanel Grid.Row="1">
<TextBox Text="{Binding ElementName=lbTodoList, Path=SelectedItem.Name}"/>
<Button Command="{Binding ChangeCurrentView}">Click Me</Button>
<Button Command="{Binding ChangeCurrentView}">Click Me</Button>
<ListBox Name="lbTodoList" HorizontalContentAlignment="Stretch"
ItemsSource="{Binding ThisList1}"
Width="200"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Width="auto" Foreground="Black" Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
不要忘记使用代码隐藏设置 DataContext
:
this.DataContext=this;
或通过Xaml
DataContext="{Binding RelativeSource={RelativeSource Self}}"
并考虑实现 INotifypropertyChanged
接口以从视图模型自动更新 UI。