【问题标题】:WPF TabControl 和数据模板
【发布时间】:2022-01-22 10:11:25
【问题描述】:

对 WPF 世界来说还算新鲜。当它是根下的基本元素树时,我过去将数据绑定到视图模型就很好了。

这样可以正常工作:

<TextBox x:Name="emailTxt" Grid.Row="6" Grid.Column="2" Text="{Binding Path=Email}">

我的电子邮件字符串对象绑定并适当地显示在上面的文本框中。

但是,当使用 DataTemplate 时似乎没有任何效果。我不确定它是否是未正确实现 DataTemplate 或 TabControl 的组合。我已经尝试根据其他来源(在模板上设置 DataType 等)稍微切换逻辑,但这并不好。

这是我目前拥有的示例(抱歉,模板很长,需要一个 3x40 单元格的大网格):此模板存在于 Windows.Resources 下。

<DataTemplate x:Key="commGridTemplate" DataType="{x:Type ViewModels:CommViewModel}">
        <ScrollViewer Margin="10,30,60,30">
            <Grid Name="CommGrid" IsSharedSizeScope="True"> <!-- DataContext="{Binding}" -->
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Name="RowBoxCol" Width="0.2*"></ColumnDefinition>
                    <ColumnDefinition Name="ChannelCol" Width="1*"></ColumnDefinition>
                    <ColumnDefinition Name="FrequencyCol" Width="1*"></ColumnDefinition>
                    <ColumnDefinition Name="DescriptionCol"  Width="2*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <!--#region Row Definitions-->
                    <RowDefinition Height="20" SharedSizeGroup="CommRow"></RowDefinition> <!-- Header Row-->
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
                    <!--#endregion Row Definitions-->
                </Grid.RowDefinitions>
                
                <!-- Header boxes-->
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="0" IsReadOnly="True"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="1" Grid.Row="0" HorizontalContentAlignment="Center" IsReadOnly="True" Text="Channel"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="2" Grid.Row="0" HorizontalContentAlignment="Center" IsReadOnly="True" Text="Frequency"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="3" Grid.Row="0" HorizontalContentAlignment="Center" IsReadOnly="True" Text="Description"></TextBox>
                
                <!--#region Content boxes-->
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="1" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox x:Name="ExampleBox" Text="{Binding Path=ExampleText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="1" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="1" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="1" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="2" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="2" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="2" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="2" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="3" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="3" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="3" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="3" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="4" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="4" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="4" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="4" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="5" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="5" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="5" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="5" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="6" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="6" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="6" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="6" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="7" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="7" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="7" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="7" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="8" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="8" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="8" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="8" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="9" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="9" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="9" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="9" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="10" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="10" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="10" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="10" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="11" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="11" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="11" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="11" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="12" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="12" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="12" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="12" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="13" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="13" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="13" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="13" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="14" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="14" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="14" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="14" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="15" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="15" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="15" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="15" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="16" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="16" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="16" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="16" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="17" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="17" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="17" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="17" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="18" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="18" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="18" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="18" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="19" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="19" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="19" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="19" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="20" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="20" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="20" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="20" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="21" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="21" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="21" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="21" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="22" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="22" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="22" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="22" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="23" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="23" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="23" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="23" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="24" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="24" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="24" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="24" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="25" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="25" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="25" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="25" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="26" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="26" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="26" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="26" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="27" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="27" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="27" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="27" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="28" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="28" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="28" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="28" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="29" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="29" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="29" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="29" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="30" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="30" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="30" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="30" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="31" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="31" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="31" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="31" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="32" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="32" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="32" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="32" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="33" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="33" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="33" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="33" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="34" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="34" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="34" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="34" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="35" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="35" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="35" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="35" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="36" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="36" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="36" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="36" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="37" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="37" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="37" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="37" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="38" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="38" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="38" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="38" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="39" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="39" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="39" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="39" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="40" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
                <TextBox Grid.Column="1" Grid.Row="40" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="2" Grid.Row="40" HorizontalContentAlignment="Center"></TextBox>
                <TextBox Grid.Column="3" Grid.Row="40" HorizontalContentAlignment="Center"></TextBox>
                <!--#endregion-->
                
                <ContentPresenter Content="{Binding}"/>
            </Grid>
        </ScrollViewer>
    </DataTemplate>

<Grid Name="MainWindowGrid">
    <GroupBox Style="{DynamicResource GroupBoxStyleNoHeader}" BorderThickness="1" Margin="10,25,10,10">
        <Grid Name="GroupBoxGrid">
            <Grid.RowDefinitions>
                <RowDefinition x:Name="tabControlRow" Height="14*"></RowDefinition>
                <RowDefinition x:Name="formButtonsRow" SharedSizeGroup="A"></RowDefinition>
            </Grid.RowDefinitions>
            <TabControl Name="CommTabs" Grid.Row="0" Margin="5,10,5,0" ContentTemplate="{StaticResource commGridTemplate}">

                <TabItem Name="UHFTab" Header="UHF" Padding="15,2,15,2"/>
                <TabItem Name="VHFTab" Header="VHF" Padding="15,2,15,2"/>
            </TabControl>

现在我只是试图让 Grid 1,1 处的 ExampleBox 绑定到我的 CommViewModel 中存在的 ExampleText 对象。我在顶部为 xmlns:ViewModels="clr-namespace:CommGUI.ViewModels" 设置了一个命名空间,因此希望它可以访问。我还将 ViewModel 设置为 MainWindow.xaml.cs 中主窗口的 DataContext

 ViewModels.CommViewModel viewModel = new ViewModels.CommViewModel(Convert.ToString(id));
        this.DataContext = viewModel;

任何意见将不胜感激。

**编辑:**回应评论:我的网格是由所有文本框组成的 3x40。 3 列,40 行。顶行由禁用的框组成,用作每列的标题。这些列是频道、频率和说明。我想每一行都将包含来自 CommViewModel 的绑定对象的数据。

GUI Mock

【问题讨论】:

  • 让我们后退一步...您的网格是 3x40?编辑您现有的帖子,不要将其放在评论中。您能否发布您尝试在网格中呈现的内容(可能是数据网格)的上下文。为什么是 3x40。您可以发布单个记录的上下文示例吗?还是每列代表 3 行上的重复数据?这可能会帮助我或其他人更好地为您的需求提供解决方案。只需给我们您正在尝试获取输出的上下文。我看到了频道、频率和描述,但没有示例数据,即使要显示的文字也是如此。
  • 更新了更多信息。
  • 你肯定想使用DataGrid。访问链接并向下滚动到备注部分以查找渲染示例的图像。作为开发人员或程序员,当您必须编写重复的代码时,您应该始终感到警惕,尤其是在这种重复程度的情况下。

标签: wpf data-binding datatemplate


【解决方案1】:

我认为你的做法是错误的。您可能需要创建一个列表,其中包含您需要的 3 个部分...频率、描述和您想要的任何其他内容。通过 getter/setter 将 List 公开。

在您的表单中,创建一个 DataGrid 控件并将其项目源设置为 List,您就差不多完成了。

我创建了一个名为 Help1 的示例项目,因此请更改上下文,或将其复制粘贴到您的项目中以查看它是否有效。下面是 MainWindow.xaml,显示了数据网格的使用。

<Window 
    x:Class="Help1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Help1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">

    <Grid>

        <DataGrid ItemsSource="{Binding AllMyChannels}"
            SelectedItem="{Binding JustTheOneChosenInTheGrid}" 
            AutoGenerateColumns="False">

            <DataGrid.Columns>
                <DataGridTextColumn Header="Channel"  Width="70"
                    CanUserSort="True" Binding="{Binding Channel}" />


                <DataGridTextColumn Header="Frequency"  Width="100"
                    CanUserSort="True" Binding="{Binding Frequency}" />

                <DataGridTextColumn Header="Description"  Width="200"
                    CanUserSort="True" Binding="{Binding Description}" />
            </DataGrid.Columns>
        </DataGrid>
        
    </Grid>
</Window>

现在,通过 MainWindow.xaml.cs 的代码隐藏

using System;
using System.Collections.Generic;
using System.Windows;

namespace Help1
{
    public partial class MainWindow 
    {
        public MainWindow()
        {
            prepareChannels();
            DataContext = this;
            InitializeComponent();
        }

        public List<YourStuff> AllMyChannels { get; set; }
        private void prepareChannels()
        {
            AllMyChannels = new List<YourStuff>();
            AllMyChannels.Add(new YourStuff { Channel = "x1", Frequency = 98.7m, Description = "Some radio station" });
            AllMyChannels.Add(new YourStuff { Channel = "y2", Frequency = 16.7m, Description = "Another Thing" });
            AllMyChannels.Add(new YourStuff { Channel = "z9", Frequency = 2320.3m, Description = "who knows" });
            // continue with all other frequency / items you have, or pull from some database somewhere.
        }

        // a public object reference to hold whatever the current selected item would be wihtin a datagrid
        private YourStuff _justTheOneChosenInTheGrid = null;
        public YourStuff JustTheOneChosenInTheGrid
        {
            get { return _justTheOneChosenInTheGrid; }
            set { _justTheOneChosenInTheGrid = value;

                // anything you might want to do with the record the user just changed to
                MessageBox.Show($"Just selected Channel: {_justTheOneChosenInTheGrid.Channel}");
            }
        }
    }

    // define your own class listing the things you want to present to the user.
    public class YourStuff
    {
        public string Channel { get; set; } = "";
        public Decimal Frequency{ get; set; } = 0;
        public string Description { get; set; } = "";
    }
}

【讨论】:

  • 感谢您的简洁回答!虽然它没有回答“如何将视图模型绑定到 DataTemplate”,但它帮助我思考了我试图完成的根本问题。您使用 Datagrid 并将 ItemSource 适当地绑定到数据对象的方法效果很好。我现在什至不需要 DataTemplate。
  • @whizkid75,很高兴它有效。现在,我给你的是一个基线。您可以查看样式模板,您可以使用格式和其他内容使您的数据网格看起来像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
相关资源
最近更新 更多