DataFids的WPF Tabcontrol

时间:2013-09-16 20:50:12

标签: c# wpf wpfdatagrid

我试图创建一个数据绑定Tabcontrol,其中每个TabItem包含一个Datagrid,也是数据绑定。

我的数据结构是这样的:

    public class Exercise
{
    public Guid ExerciseID { get; protected set; }
    public string ExerciseName { get; set; }
    public List<Set> Sets { get; set; }
}

    public class Set
{
    public Guid SetID { get; protected set; }
    public decimal Weight { get; set; }
    public decimal Reps { get; set; }
    public bool MaxEffort { get; set; }
}

我尝试将List<Exercise>绑定到TabControl,并将每个练习的List<Set>绑定到每个标签内的DataGrid,显示&#34;权重&#34;和#34;代表&#34;两列中的属性。我当前的XAML代码如下,相关部分标有。

<Window x:Class="MyWorkouts.WorkoutViewer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WorkoutViewer" Height="424" Width="708"
    x:Name="WorkoutDisplay">
<Grid DataContext="{DynamicResource WorkoutToDisplay}">
    <Calendar SelectedDate="{Binding WorkoutDate}" Height="180" HorizontalAlignment="Left" Margin="8,12,0,0" Name="calendar1" VerticalAlignment="Top" Width="230" />
    <StackPanel Height="94" HorizontalAlignment="Left" Margin="12,182,0,0" Name="spProperties" VerticalAlignment="Top" Width="238">
        <StackPanel Height="30" Name="spProgram" Width="232" Orientation="Horizontal">
            <Label Content="Workout Program:" Height="25" Name="lblProgram" Width="104" />
            <TextBox Text="{Binding WorkoutProgram}" Height="25" Name="tbProgram" Width="120" />
        </StackPanel>
        <StackPanel Height="30" Name="spType" Width="232" Orientation="Horizontal">
            <Label Content="Workout Type:" Height="25" Name="lblWorkoutType" Width="104" />
            <TextBox Text="{Binding WorkoutType}" Height="25" Name="tbWorkoutType" Width="120" />
        </StackPanel>
        <StackPanel Height="30" Name="spVenue" Width="232" Orientation="Horizontal">
            <Label Content="Workout Venue:" Height="25" Name="lblVenue" Width="104" />
            <TextBox Text="{Binding WorkoutVenue}" Height="25" Name="tbVenue" Width="120" />
        </StackPanel>
    </StackPanel>

    <TabControl Height="365" HorizontalAlignment="Left" Margin="260,10,0,0" Name="TCWorkoutView" VerticalAlignment="Top" Width="425">
        <TabItem Header="Workout View" Name="TIView">
            <StackPanel Height="335" HorizontalAlignment="Left" Name="spExercises" VerticalAlignment="Top" Width="410" Grid.ColumnSpan="2">
                <ItemsControl ItemsSource="{Binding Exercises}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Expander Header="{Binding ExerciseName}" BorderThickness="1" BorderBrush="DarkBlue">
                                <ItemsControl ItemsSource="{Binding Sets}" DisplayMemberPath="WeightForReps" BorderThickness="1" BorderBrush="Gray"/>
                            </Expander>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </TabItem>
        <!--The below section is the one in question-->
        <TabItem Header="Edit Workout">
            <TabControl ItemsSource="{Binding Path=Exercises}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <DataGrid ItemsSource="{Binding Path=Sets}" AutoGenerateColumns="False">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Weight" Binding="{Binding Path=Weight}"/>
                                <DataGridTextColumn Header="Reps" Binding="{Binding Path=Reps}"/>
                            </DataGrid.Columns>
                        </DataGrid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </TabControl>
        </TabItem>
    </TabControl>
</Grid>

我已经在每个标签中正确显示了ExerciseName属性(生成了正确数量的标签,但根本没有数据网格,而Tab只是说MyWorkouts.Exercise( MyWorkouts是命名空间。

对于一些背景知识,这应该是&#34;编辑&#34;用于锻炼记录程序的屏幕,我已经使用扩展器的堆叠面板正确显示了显示视图 - 我需要任何可编辑的解决方案,并且在数据网格中进行的更改反映在相应的类对象中 - 我希望自己能够解决这个问题,但如果这种方法不起作用,请告诉我!

编辑:我的完整XAML代码现在列在上面

1 个答案:

答案 0 :(得分:1)

需要设置TabControl.ContentTemplate而不是ItemsControl.ItemTemplate。切换后,我可以使用DisplayMemberPath属性,其他一切都有效!

我需要的XAML代码是:

<TabControl ItemsSource="{Binding Path=Exercises}" DisplayMemberPath="ExerciseName">
                <TabControl.ContentTemplate>
                    <DataTemplate>
                            <DataGrid ItemsSource="{Binding Path=Sets}" AutoGenerateColumns="False">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="Weight" Binding="{Binding Path=Weight}"/>
                                    <DataGridTextColumn Header="Reps" Binding="{Binding Path=Reps}"/>
                                </DataGrid.Columns>
                            </DataGrid>
                    </DataTemplate>
                </TabControl.ContentTemplate>
            </TabControl>