首页 > 解决方案 > 在 WPF 数据模板中,我错过了什么?数据显示不正确

问题描述

  1. 我使用“SecondCondition”类作为基本数据单元。它有3个公共财产。条件颜色、条件名称、条件 ID
  2. ObservableCollection 'SearchConditionList' 用作数据列表。
  3. 我做了一个如下的数据模板绑定。
    < 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>
  1. 我在我的列表框中使用了数据模板。
    < 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>

结果如下。

在此处输入图像描述

标签: wpfbindingdatatemplate

解决方案


这是我基于您的源代码的实现。

模型

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 不会明确吐出异常。但是您应该能够在输出窗口中看到绑定错误。所以你可以找到线索。

请将您的实现与我的进行比较。


推荐阅读