ContentControl中的DataGrid,它使用ContentTemplateSelector

时间:2017-01-14 10:27:39

标签: c# wpf datagrid contentcontrol

我目前正在尝试构建一个表单,该表单根据输入显示具有不同列和数据的不同数据网格。我正在选择合适的DataTemplate并且列正确显示。我将DataContext绑定到ContentControl。但是,数据不会显示在DataGrid中。 这是app.xaml文件中的一个模板。

<DataTemplate x:Key="templateHola1" DataType="{x:Type ContentControl}">
        <DataGrid DataContext="{Binding}">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Savings">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Savings}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Total Income">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding TotalIncome}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </DataTemplate>

这是表格

<Window.Resources>
    <local:HolaContentTemplateSelector x:Key="contentTemplateSelector" />
</Window.Resources>

<StackPanel Orientation="Vertical">
    <ComboBox SelectionChanged="ComboBox_SelectionChanged_1" DropDownClosed="ComboBox_DropDownClosed_1">
        <ComboBoxItem>Content 1</ComboBoxItem>
        <ComboBoxItem>Content 2</ComboBoxItem>
        <ComboBoxItem>Content 3</ComboBoxItem>
    </ComboBox>
    <ContentControl x:Name="ctrAbc" Content="{Binding}" ContentTemplateSelector="{StaticResource contentTemplateSelector}" />

    <TextBox Text="{Binding Savings}" />
</StackPanel>

最后一个TextBox绑定有效,而DataGrid绑定则没有。请照亮我。

修改

Savings和TotalIncome是类中的属性,其列表用作数据源。

public class NetIncome : INotifyPropertyChanged
{
    private int totalIncome = 5000;
    private int rent = 2000;
    private int food = 0;
    private int misc = 0;
    private int savings = 0;
    public NetIncome()
    {
        savings = totalIncome - (rent + food + misc);
    }

    public int TotalIncome
    {
        get
        {
            return totalIncome;
        }
        set
        {
            if (TotalIncome != value)
            {
                totalIncome = value;
                OnPropertyChanged("TotalIncome");
            }
        }
    }
    public int Rent
    {
        get
        {
            return rent;
        }
        set
        {
            if (Rent != value)
            {
                rent = value;
                OnPropertyChanged("Rent");
                UpdateSavings();
            }
        }
    }
    public int Food
    {
        get
        {
            return food;
        }
        set
        {
            if (Food != value)
            {
                food = value;
                OnPropertyChanged("Food");
                UpdateSavings();
            }
        }
    }
    public int Misc
    {
        get
        {
            return misc;
        }
        set
        {
            if (Misc != value)
            {
                misc = value;
                OnPropertyChanged("Misc");
                UpdateSavings();
            }
        }
    }
    public int Savings
    {
        get
        {
            return savings;
        }
        set
        {
            if (Savings != value)
            {
                savings = value;
                OnPropertyChanged("Savings");
                UpdateSavings();
            }
        }
    }

    private void UpdateSavings()
    {
        Savings = TotalIncome - (Rent + Misc + Food);
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(String info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
}

0 个答案:

没有答案