Scottintexas
Well-known member
I want to add a button to a row in a ListView. The row is simply several <TextBlocks> as defined in a <DataTemplate> in a <ResouceDictionary>. The command is in the ViewModel. I am missing a "connection" to the command in the ViewModel. If I add the button right to the View I can easily bind it to the code. But separating it to the ResourceDictionary makes the command invisible.
Then in the MainWindowViewModel there is the code to handle the command.
A break point at the getter never gets hit. Unlike the SelectFilesCommand. That and the other buttons work great. But they are not being created in a DataTemplate.
Thanks for looking.
XAML in MainWindowView:
<Window.Resources>
<ResourceDictionary Source="./ViewResources.xaml" />
</Window.Resources>
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
<ListView ItemsSource="{Binding Ports, Mode=OneWay}"
ItemTemplate="{StaticResource ComportTemplate}"/>
ViewResources:
<DataTemplate x:Key="ComportTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Grid.Row="0"
Grid.Column="0"
Background="Red"
Height="10"
Width="10"
Margin="10"
VerticalAlignment="Center"
Command="{Binding TestPortCommand}"
CommandParameter="{Binding ElementName=PortName, Path=Text}"/>
<TextBlock x:Name="PortName"
Grid.Column="1"
VerticalAlignment="Center"
Margin="1,4,4,4">
</TextBlock>
</Grid>
</DataTemplate>
Then in the MainWindowViewModel there is the code to handle the command.
MainWindowViewModel:
private RelayCommand selectFilesCommand;
private RelayCommand uploadCommand;
private RelayCommand printFileCommand;
private RelayCommand removeFileCommand;
private RelayCommand testPortCommand;
/// <summary>
///ICommand for the Select Files Button
/// </summary>
public ICommand SelectFilesCommand
{
get
{
if (selectFilesCommand == null)
{
selectFilesCommand = new RelayCommand(param => SelectFiles());
}
return selectFilesCommand;
}
}
/// <summary>
/// Button to test communications with the serial port.
/// </summary>
/// <param name="port"></param>
public ICommand TestPortCommand
{
get
{
if (testPortCommand == null)
{
testPortCommand = new RelayCommand(param => TestComPort(param));
}
return testPortCommand;
}
}
A break point at the getter never gets hit. Unlike the SelectFilesCommand. That and the other buttons work great. But they are not being created in a DataTemplate.
Thanks for looking.