wpf - 在 WPF 数据模板中,我错过了什么?数据显示不正确
问题描述
- 我使用“SecondCondition”类作为基本数据单元。它有3个公共财产。条件颜色、条件名称、条件 ID
- ObservableCollection 'SearchConditionList' 用作数据列表。
- 我做了一个如下的数据模板绑定。
< Model:SearchCondition x:Key="SearchCondition" />
< DataTemplate x:Key="ConditionSelector">
< StackPanel >
< xctk:ColorPicker x:Name="ConditionColorPicker"
SelectedColor="{Binding Path=ConditionColor,
Mode=TwoWay}">
< /xctk:ColorPicker>
< CheckBox x:Name="ConditionCheckbox"
Content="{Binding Path=ConditionName,
Mode=TwoWay}" />
< /StackPanel>
- 我在我的列表框中使用了数据模板。
< ListBox ItemsSource="{Binding Path=SearchConditionList}"
ItemTemplate="{StaticResource ConditionSelector}">
< /ListBox>
结果,我得到的空白模板数量与项目列表一样多。但它不显示颜色和名称等属性。我用作参考文章的内容几乎相同并且代码有效,但我的不是。我该如何解决这个问题?
以下是我的参考。 https://docs.microsoft.com/ko-kr/dotnet/framework/wpf/data/data-templating-overview
谢谢你。
PS 当我更改如下代码时,常量字符串显示得非常好,但绑定值不是。
<StackPanel DataContext="{Binding Path=SearchConditionList}">
<ListBox ItemsSource="{Binding}"
>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock Text="Why this doens't show bound value?"/>
<TextBlock Text=" : " />
<TextBlock Text="{Binding Path=ConditionName}"/>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
结果如下。
解决方案
这是我基于您的源代码的实现。
模型
public class SearchCondition
{
public Color ConditionColor { get; set; }
public string ConditionName { get; set; }
public string ConditionID { get; set; }
public SearchCondition(Color color, string name, string id)
{
ConditionColor = color;
ConditionName = name;
ConditionID = id;
}
}
视图模型
public class ViewModel
{
public ObservableCollection<SearchCondition> SearchConditionList { get; set; }
public ViewModel()
{
SearchConditionList = new ObservableCollection<SearchCondition>();
SearchConditionList.Add(new SearchCondition(Colors.Red, "Red", "001"));
SearchConditionList.Add(new SearchCondition(Colors.Green, "Green", "002"));
}
}
ViewModel 绑定到代码隐藏中的视图。
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
好的.. 现在这是 XAML。
<Window x:Class="DataBindingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="ConditionSelector">
<StackPanel Orientation="Horizontal">
<xctk:ColorPicker x:Name="ConditionColorPicker" SelectedColor="{Binding Path=ConditionColor, Mode=TwoWay}" />
<CheckBox x:Name="ConditionCheckbox" Content="{Binding Path=ConditionName, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</Grid.Resources>
<ListBox ItemsSource="{Binding Path=SearchConditionList}" ItemTemplate="{StaticResource ConditionSelector}" />
</Grid>
老实说,除非我看到您的完整源代码,否则我无法弄清楚问题出在哪里。Visual Studio 不会明确吐出异常。但是您应该能够在输出窗口中看到绑定错误。所以你可以找到线索。
请将您的实现与我的进行比较。
推荐阅读
- python - 创建基于 Pandas.DataFrame 的类,使用 pandas.read_csv() 函数进行初始化
- git - 有没有办法检查另一个分支中存在的分支中的最后一次提交?
- c++ - 如何让 mingw64 链接到 Windows 上的 OpenAL
- javascript - @Url.Action 和 JavaScript
- javascript - 你如何在php中的输入之间切换?
- python - Python tkinter 在类或帧之间传递变量
- c# - 以编程方式获取 BIOS 引导菜单键
- python - Geoviews + Datashader在投影点时很慢
- mysql - 未来预期销售查询的功能将不会运行 - MySQL
- powershell - 如何捕获异常以使异常消息不在控制台上打印?