Я новичок в WPF, ниже моя текущая реализация XAML:
<Window x:Class="TestingWPFPage.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:col="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:local="clr-namespace:TestingWPFPage"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="600">
<Window.Resources>
<col:ArrayList x:Key="arrList">
<col:DictionaryEntry Key="Fixed" Value="1"/>
<col:DictionaryEntry Key="Variable" Value="2"/>
</col:ArrayList>
</Window.Resources>
<Border Padding="2">
<Grid>
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto">
<DataGrid Margin="5,5,5,40" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" VerticalAlignment="Stretch" Height="400" Name="dataGrid1" ItemsSource="{Binding GridCollection}">
<DataGrid.Columns>
<!-- 1st Column - Select All Checkbox -->
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<CheckBox Content="Select All" x:Name="headerCheckBox" />
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkSelectAll" VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding IsChecked, ElementName=headerCheckBox, Mode=OneWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- 2nd Column - Consist - Select Fixed or Variable -->
<DataGridComboBoxColumn Header="Consist" Width="*" SelectedValueBinding="{Binding Active}" ItemsSource="{StaticResource arrList}" DisplayMemberPath="Key" SelectedValuePath="Value"/>
<!-- 3rd Column - Vehicle Count -->
<DataGridTextColumn Header="Vehicle Count" Width="*" Binding="{Binding VehicleCount}"/>
<!-- 4th Column - Vehicles List -->
<DataGridTemplateColumn Header="Vehicles" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel x:Name ="vehicleStackPanel" Orientation="Horizontal" Margin="3"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- 5th Column - VATCs Selection Checkbox -->
<DataGridTemplateColumn Header="VATCs" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="3">
<CheckBox x:Name="selectVATCA" IsChecked="False" Margin="3"/>
<Label Content="A" Margin="3"/>
<CheckBox x:Name="selectVATCB" IsChecked="False" Margin="3"/>
<Label Content="B" Margin="3"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- 6th Column - Region -->
<DataGridTextColumn Header="Region" Width="0.8*" Binding="{Binding Region}"/>
<!-- 7th Column - Segment -->
<DataGridTextColumn Header="Segment" Width="0.8*" Binding="{Binding Segment}"/>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
<Button Height="20" Width="60" HorizontalAlignment="Left" Name="InitTrain" Click="InitTrain_Click" VerticalAlignment="Bottom" Margin="38,0,0,10">Initialize</Button>
</Grid>
</Border>
Я ищу реализацию, в которой панели столбцов должны быть добавлены в столбцы Транспортные средства .
Я пытаюсь получить доступ к контрольной ссылке из xaml в файл кода, но она не может найти ссылку.
Ниже мой код позади
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
int vehicleCount = 0;
int region = 0;
int segment = 0;
TextBox txtbox = new TextBox();
// Consist Combobox Items
public enum Consist { Fixed, Variable };
public int VehicleCount
{
get { return vehicleCount; }
set
{
vehicleCount = value;
OnPropertyChanged("VehicleCount");
}
}
public int Region
{
get { return region; }
set
{
region = value;
OnPropertyChanged("Region");
}
}
public int Segment
{
get { return segment; }
set
{
segment = value;
OnPropertyChanged("Segment");
}
}
public Consist ConsistList { get; set; }
public string Vehicles { get; set; }
public string VATCs { get; set; }
public MainWindow()
{
InitializeComponent();
for (int i = 1; i <= vehicleCount; i++)
{
txtbox.Text = "textbox" + Convert.ToString(i);
this.vehicleStackPanel.Childern.Add(txtbox);
}
}
private void InitTrain_Click(object sender, RoutedEventArgs e)
{
}
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Есть ли способ сделать это в XAML, или нам нужен код для этого, если так - как мне действовать?