This is my first attempt at using WPF for building a C# application; I have always used Windows Forms in the past. I am running into an error with what seems to be a simple assignment. When the following assignment of a button's .IsEnabled
property is attempted, the exception System.Reflection.TargetIncovationException
occurs in PresentationFramework.dll. When I press Break, I just get the Source Not Available window and the option ti view disassembly information.
private void txtFileLoc_TextChanged(object sender, TextChangedEventArgs e) {
if (txtFileLoc.Text.EndsWith(".txt"))
btnExecute.IsEnabled = true;
else
btnExecute.IsEnabled = false;
}
I have verified that it is the assignment throwing the exception by replacing them with Console.WriteLine to debug.
EDIT: As requested, here is the program XAML & CS
<Window x:Name="winMain" x:Class="IP_Extractor_2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="IP Extractor" Height="250" Width="410" ResizeMode="CanMinimize" Background="#FFECEFF4">
<Grid x:Name="grdMain">
<Grid.RowDefinitions>
<RowDefinition Height="39*"/>
<RowDefinition Height="161*"/>
<RowDefinition Height="21*"/>
</Grid.RowDefinitions>
<ProgressBar x:Name="prgProgress" Margin="0,0,0,0" Grid.Row="2" VerticalAlignment="Bottom" Height="17" Foreground="#FF7A6BA6" Value="50"/>
<TextBox x:Name="txtFileLoc" HorizontalAlignment="Left" Height="20" Margin="10,10,0,0" Text="Browse for a Text File" VerticalAlignment="Top" Width="290" TextChanged="txtFileLoc_TextChanged"/>
<Button x:Name="btnBrowse" Content="Browse" HorizontalAlignment="Left" Margin="305,10,0,0" VerticalAlignment="Top" Width="52" Height="20" Click="btnBrowse_Click"/>
<Button x:Name="btnExecute" Content="Go" HorizontalAlignment="Left" Margin="362,10,0,0" VerticalAlignment="Top" Width="22" Height="20" Click="btnExecute_Click" IsEnabled="False"/>
<ListBox x:Name="lboResults" HorizontalAlignment="Left" Height="151" Margin="10,0,0,0" Grid.Row="1" VerticalAlignment="Top" Width="374"/>
</Grid>
</Window>
CODE:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace IP_Extractor_2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
OpenFileDialog openFileDialog1;
public MainWindow() {
InitializeComponent();
}
private void btnBrowse_Click(object sender, RoutedEventArgs e) {
// Create an instance of the open file dialog box.
openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = "Text Files (.txt)|*.txt";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false;
// Call the ShowDialog method to show the dialog box.
bool? userClickedOK = openFileDialog1.ShowDialog();
// Process input if the user clicked OK.
if (userClickedOK == true)
txtFileLoc.Text = openFileDialog1.FileName;
}
private void txtFileLoc_TextChanged(object sender, TextChangedEventArgs e) {
if (txtFileLoc.Text.EndsWith(".txt"))
btnExecute.IsEnabled = true;
else
btnExecute.IsEnabled = false;
}
}
}
Check for the null/empty string before setting and see if that changes things.
if (btnExecute != null)
{
btnExecute.IsEnabled = ((txtFileLoc != null) &&
(string.IsNullOrEmpty(txtFileLoc.Text) == false) &&
(txtFileLoc.Text.ToLower().EndsWith(".txt")));
}
Edit
Although code behind works and could be a valid way of doing it, it is not the standard way. WPF is all about binding and INotifyProperty changed events related.
I would have my button bound to a View Model Boolean which would be on the page's data context (which would be inherited by the button) and bound such as
<Button IsEnabled="{Binding IsExecuteButtonOperational }"/>
I recommend you begin to look into MVVM programming paradigm (Think of it as way of separating concerns of business logic form view logic). I provide an example on my blog Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding.which could get you started.