WPF TabControl 和数据模板
WPF TabControl and Data Templates
对 WPF 世界还很陌生。过去,当它是根下的基本元素树时,我已经将数据绑定到视图模型。
像这样效果很好:
<TextBox x:Name="emailTxt" Grid.Row="6" Grid.Column="2" Text="{Binding Path=Email}">
我的电子邮件字符串 object 正确绑定并显示在上面的文本框中。
但是,使用 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 object。我在顶部为 xmlns:ViewModels="clr-namespace:CommGUI.ViewModels" 设置了一个命名空间,希望它可以访问。我还将 ViewModel 设置为 MainWindow.xaml.cs
中主要 window 的 DataContext
ViewModels.CommViewModel viewModel = new ViewModels.CommViewModel(Convert.ToString(id));
this.DataContext = viewModel;
如有任何意见,我们将不胜感激。
**编辑:** 回应评论:我的网格是一个由所有文本框组成的 3x40。 3 列,40 行。顶行由禁用框组成,作为每列的 header。这些列是频道、频率和描述。我想每一行都将包含来自 CommViewModel 的绑定 object 的数据。
GUI Mock
我认为你的做法是错误的。您可能需要的是创建一个列表,其中包含您需要的 3 个部分……频率、描述和您想要的任何其他内容。通过 getter/setter.
创建列表<> public
在您的表单中,创建一个 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; } = "";
}
}
对 WPF 世界还很陌生。过去,当它是根下的基本元素树时,我已经将数据绑定到视图模型。
像这样效果很好:
<TextBox x:Name="emailTxt" Grid.Row="6" Grid.Column="2" Text="{Binding Path=Email}">
我的电子邮件字符串 object 正确绑定并显示在上面的文本框中。
但是,使用 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 object。我在顶部为 xmlns:ViewModels="clr-namespace:CommGUI.ViewModels" 设置了一个命名空间,希望它可以访问。我还将 ViewModel 设置为 MainWindow.xaml.cs
中主要 window 的 DataContext ViewModels.CommViewModel viewModel = new ViewModels.CommViewModel(Convert.ToString(id));
this.DataContext = viewModel;
如有任何意见,我们将不胜感激。
**编辑:** 回应评论:我的网格是一个由所有文本框组成的 3x40。 3 列,40 行。顶行由禁用框组成,作为每列的 header。这些列是频道、频率和描述。我想每一行都将包含来自 CommViewModel 的绑定 object 的数据。
GUI Mock
我认为你的做法是错误的。您可能需要的是创建一个列表,其中包含您需要的 3 个部分……频率、描述和您想要的任何其他内容。通过 getter/setter.
创建列表<> public在您的表单中,创建一个 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; } = "";
}
}