简体   繁体   English

WPF Tabgrid的Datagrids

[英]WPF Tabcontrol of Datagrids

I'm trying to create a databound Tabcontrol, where each TabItem contains a Datagrid, also databound. 我正在尝试创建一个数据绑定的Tabcontrol,其中每个TabItem都包含一个Datagrid,也包含数据绑定。

My data structure is something like this: 我的数据结构是这样的:

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

I'm trying to bind a List<Exercise> to a TabControl, and the List<Set> for each Exercise to the DataGrid within each Tab, displaying the "Weight" and "Reps" properties in two columns. 我试图将List<Exercise>绑定到TabControl,并将每个练习的List<Set>绑定到每个Tab中的DataGrid,在两列中显示“ Weight”和“ Reps”属性。 My current XAML code is below, with the relevant section labelled. 我当前的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>

I've got as far as having the ExerciseName property correctly displayed in each Tab (with the correct number of tabs generated, but there's no datagrid at all, instead the Tab just says MyWorkouts.Exercise (MyWorkouts is the namespace). 我已经尽可能地在每个选项卡中正确显示了ExerciseName属性(生成了正确数量的选项卡,但是根本没有数据网格,而是选项卡仅显示MyWorkouts.Exercise(MyWorkouts是名称空间)。

For a bit of background, this is meant to be the "Edit" screen for a workout logging program, and I've got the display view working correctly with a stackpanel of expanders - I need whatever solution I have to be editable, and have the changes made in the datagrid reflected in the appropriate class objects - I hope to work this out myself, but if this approach won't work, please let me know! 出于某种背景,这应该是锻炼日志记录程序的“编辑”屏幕,并且我已使显示视图与一堆扩展器一起正常工作-我需要任何必须可编辑的解决方案,并具有datagrid中所做的更改反映在适当的类对象中-我希望自己解决这个问题,但是如果这种方法行不通,请告诉我!

EDIT: My full XAML code is now listed above 编辑:我的完整XAML代码现在在上面列出

Needed to set the TabControl.ContentTemplate rather than the ItemsControl.ItemTemplate . 需要设置TabControl.ContentTemplate而不是ItemsControl.ItemTemplate Once I switched that, I could use the DisplayMemberPath property, and everything else worked! 切换之后,就可以使用DisplayMemberPath属性,其他所有功能都可以使用!

The XAML code I needed was: 我需要的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM