我已经找到了,但找不到答案所以我转向专家。我为这个长度道歉,但没有看到一切,我相信很难将其纳入背景。
在WPF窗口中,我有一个制表符控件。选项卡的数量由列表中的项目数决定。每个选项卡都有3个TextBox,一个ScrollViewer和3个按钮。我不使用MVVM。 TextBoxes和ScrollViewer的绑定很好。我似乎无法弄清楚如何连接一个Button Command处理程序。我甚至尝试过最简单的方法,创建一个RoutedCommand并使用它作为演示here。我注意到的一件事是我无法从智能感知“看到”这些按钮。
我有一个从ICommand派生的ButtonCommand类:
class ButtonCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public event EventHandler CanExecuteChanged;
public ButtonCommand(Action<object> execute)
: this(execute, null)
{
}
public ButtonCommand(Action<object> execute,
Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public void Execute(object parameter)
{
_execute(parameter);
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
有一个按钮处理程序类:
class ButtonHandler : INotifyPropertyChanged
{
public ButtonCommand StartButtonClicked { get; set; }
public ButtonHandler()
{
StartButtonClicked = new ButtonCommand((o) =>
{
int x = 1; // just something to put a breakpoint on
},
(o) =>
{
return true;
});
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnNotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我无法弄清楚我需要做些什么才能把它挂在我的按钮上!
<Window x:Class="TestOfScrollGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="this"
Title="MainWindow"
xmlns:local="clr-namespace:TestOfScrollGrid"
SizeToContent="WidthAndHeight">
<Grid Name="mainGrid">
<TabControl Name="tbcTraceTabs" ItemsSource="{Binding ChannelTraceMsgList, ElementName=this}" >
<!-- This section controls the top tab -->
<TabControl.ItemTemplate>
<DataTemplate DataType="TabItem">
<DockPanel>
<TextBlock Text="{Binding ChannelName}" />
</DockPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<!-- This section controls the content -->
<TabControl.ContentTemplate>
<DataTemplate>
<Grid Height="300" DockPanel.Dock="Top" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
<RowDefinition Height="60" />
</Grid.RowDefinitions>
<Label Margin="3,10,1,10"
HorizontalAlignment="Left"
VerticalAlignment="Center"
VerticalContentAlignment="Center"
Content="Mode:" />
<Label Grid.Column="2"
Margin="0,10,2,10"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Content="Test ID:" />
<Label Grid.Column="4"
Margin="0,10,2,10"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Content="ODM Status:" />
<TextBox x:Name="tbxTestMode"
Grid.Column="1"
Width="120"
Height="23"
Margin="2,10"
HorizontalAlignment="Right"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Background="Green"
BorderBrush="Black"
BorderThickness="0,0,0,1"
Foreground="White"
IsReadOnly="True"
Text="{Binding TestMode}"
TextWrapping="NoWrap" />
<TextBox x:Name="tbxTestID"
Grid.Column="3"
Width="120"
Height="23"
Margin="2,10"
HorizontalAlignment="Right"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Background="Green"
BorderBrush="Black"
BorderThickness="0,0,0,1"
Foreground="White"
Text="{Binding TestId}"
TextWrapping="NoWrap" />
<TextBox x:Name="tbxODMStatus"
Grid.Column="5"
Width="150"
Height="23"
Margin="2,10"
HorizontalAlignment="Right"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Background="{Binding BoxColor}"
BorderBrush="Black"
BorderThickness="0,0,0,1"
Foreground="White"
Text="{Binding OdmConnectedString}"
TextWrapping="NoWrap" />
<ScrollViewer Name="sv_TraceScrollViewer"
Grid.Row="1"
Grid.ColumnSpan="6"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding ChannelTrace}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Border Grid.Column="0"
BorderBrush="Black"
BorderThickness="1">
<TextBlock Grid.Column="0"
Margin="2,2,2,2"
Text="{Binding Path=MsgIn.Msg,
Mode=OneWay}"
TextAlignment="Left" />
</Border>
<Border Grid.Column="1"
BorderBrush="Black"
BorderThickness="1">
<TextBlock Grid.Column="1"
Margin="2,2,2,2"
Text="{Binding Path=MsgOut.Msg,
Mode=OneWay}"
TextAlignment="Left" />
</Border>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
<StackPanel Grid.Row="2" Grid.ColumnSpan="6" x:Name="spButtons"
Height="60"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal">
<Button x:Name="btnStart"
Width="70"
Height="40"
Margin="0,0,10,0"
Command="{Binding StartButtonClicked}"
Content="Start" />
<Button Name="btnNext"
Width="70"
Height="40"
Margin="10,0"
Content="Next" />
<Button Name="btnCancel"
Width="70"
Height="40"
Margin="10,0"
Content="Cancel" />
<Button Name="btnClose"
Width="70"
Height="40"
Margin="10,0"
Content="Close"
Visibility="Collapsed" />
</StackPanel>
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
</Window>
谢谢!