在 WPF 数据模板中,我错过了什么?数据显示不正确

In WPF datatemplate, What I missed? data doesn't show correctly

  1. 我使用class'SecondCondition'作为基本数据单元。它有 3 public 属性。 ConditionColor、ConditionName、ConditionID
  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

谢谢。

P.S 当我像下面这样更改代码时,常量字符串显示得很好,但绑定值却不是。

                    <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;
    }
}

ViewModel

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 绑定到 code-behind 中的视图。

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>

Screenshot

老实说,除非我看到你的完整源代码,否则我无法弄清楚问题出在哪里。 Visual Studio 没有明确吐出异常。但是您应该能够在输出 window 中看到绑定错误。这样你就可以找到线索了。

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