Name of the Control doesn't exist even if it does
I'm getting an error where chkBox1 does not exist in the current context, anyone has a solution to this?
Here is the XAML:
<ListBox ItemsSource="{Binding Files}" Margin="0,42,0,115" Name="lstBox1">
<ListBox.ItemTemplate>
<DataTemplate >
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding FileName}" Name="chkBox1" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Here is the code that has the chkBox1 in it:
private void button2_Click(object sender, RoutedEventArgs e)
{
ViewDiskModel model = this.ContentPanel.DataContext as ViewDiskModel;
if (chkBox1.IsChecked == true)
开发者_StackOverflow社区{
model.DeleteSelectedFiles.Execute(null);
MessageBox.Show("Files Successfully Deleted.");
}
else
{
MessageBox.Show("Please select a file to delete.");
}
}
If there are many Files there will be many check boxes. How would you distinguish between these when you specify a single name?
Do not refer to the View (control) in the ViewModel. Replace the string collection (filename collection) with a collection of File. Make sure the File class has two properties: Name and IsSelected.
Then bind the content of the check box to the Name and the IsChecked Property to the IsSelected property.
That way you only have to check the IsSelected property in the ViewMODEL, not in the view.
Suggestion
class File : INotifyPropertyChanged // implementation not added
{
private string _name;
public string Name
{
get { return _name; }
set
{
if(_name != value)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
private boolean _isSelected;
public boolean IsSelected
{
get { return _isSelected; }
set
{
if(_isSelected != value)
{
_isSelected = value;
OnPropertyChanged("IsSelected");
}
}
}
}
class ViewDiskModel : INotifyPropertyChanged // implementation missing
{
private ObservableCollection<File> _files;
public ObservableCollection<File> Files
{
get
{
return _files;
}
set
{
if(_files != value)
{
_files = value;
OnPropertyChanged("Files");
}
}
}
}
XAML:
<ListBox ItemsSource="{Binding Files}" Margin="0,42,0,115" Name="lstBox1">
<ListBox.ItemTemplate>
<DataTemplate >
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"
Content="{Binding FileName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Then:
private void Button2_Click(object sender, RoutedEventArgs e)
{
ViewDiskModel model = this.ContentPanel.DataContext as ViewDiskModel;
if(model.Files.Any(file => file.IsSelected))
{
model.DeleteSelectedFiles.Execute(null);
MessageBox.Show("Files Successfully Deleted.");
}
else
{
MessageBox.Show("Please select files to delete.");
}
}
DataTemplate
controls are not available by name in code behind, because they are not members of your Window
or Page
(or whatever else) class. This article has a solution. Basically, subscribe to the Loaded
event of the control you want, and in the code behind, save the event's sender
parameter, which is the control in question.
If checkbox is inside the listbox you can not access it directly. you have to do it like that:
CheckBox chkBox1 = (CheckBox)lstBox1.Controls[index_of_the_list_item].FindControl("chkBox1");
Only then you can operate using that checkbox:
if(chkBox1.checked ){}
You have to use ID to find a control though, not it's name. And somehow you need to know what index of the list you want to check...
精彩评论