Retrieving WPF control states (checkboxes, etc.) at runtime - silverlight

I have a simple Windows Phone 7 project and am not using MVVM.
I have a group of check-boxes and some other controls that determine certain statuses in the UI. Let's say on checking a check-box I get an event where I have some code and would like to know the state of other check-boxes and elements in there too. Is this possible without MVVM? The root visual does not give me the main UI class (launched in main.xaml) so there seems no way to find other peer controls.
let's say I have a Listbox with checkboxes, which are databound to an xml to a Name element.
<DataTemplate x:Key="myTemplate">
<StackPanel Orientation="Horizontal">
<CheckBox Name="chkBox" Checked="chkBox_Checked" Tag="{Binding name}">
<TextBlock Text="{Binding name}" FontSize="16" />
</CheckBox>
</StackPanel>
</DataTemplate>
This is used in a list
<ListBox x:Name="lst" SelectionMode="Single" ItemTemplate="{StaticResource myTemplate}" />
Checking an item gets me into the chkbox_Checked event. How do I determine the value of all the check-boxes in this group?
I see that lst.Items can give me the name value of the checkboxes, which is the data value they get bound to, but I am not able to cast the item to a checkBox type to inspect the isChecked value.

Provided your controls a Name in your xaml, you can reference them by this identifier in your code behind.
E.g.
<Button Content="Button" Name="button1" Click="button1_Click" />
and
private void button1_Click(object sender, RoutedEventArgs e) {
button1.Content = "Clicked";
}

Related

How do I access a windows control (checkbox) that is created dynamically in a WPF treeview?

I have a WPF Tree View with hierarchical data templates that loads objects and displays them fine. Within the children of the treeview, I am showing the "Name" of the object in the Tree View using a TextBlock, along with a Check Box next to it. Here is my code for reference:
<DockPanel Name="test1" Margin="10,10,0,10" VerticalAlignment="Stretch" Grid.Row="3" Grid.RowSpan="7" Grid.Column="0">
<DockPanel.Resources>
<local:CheckBoxCommand x:Key="cbc"></local:CheckBoxCommand>
<src:TreeViewFilter x:Key="MyList" />
<HierarchicalDataTemplate DataType="{x:Type src:TreeViewParent}" ItemsSource="{Binding Path=OrderAttributes}">
<TextBlock Text="{Binding Path=NameAndCount}" FontSize="24"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type src:OrderAttribute}" ItemsSource="{Binding Path=OrderAttributes}">
<StackPanel Name="test" Orientation="Horizontal" VerticalAlignment="Center">
<CheckBox Command="{StaticResource cbc}"
CommandParameter="{Binding Path=NameAndParent}" Visibility="{Binding Path=CheckBoxVisible}" VerticalAlignment="Center">
</CheckBox>
<TextBlock Text="{Binding Path=NameAndCount}" FontSize="16"/>
</StackPanel>
</HierarchicalDataTemplate>
</DockPanel.Resources>
<TreeView Name="treeView1" BorderThickness="2" ItemsSource="{Binding Source={StaticResource MyList}, UpdateSourceTrigger=PropertyChanged}" TreeViewItem.Selected="filterByBatchStatus"/>
</DockPanel>
When A user checks a checkbox in the tree, certain stuff happens in my application based on which checkbox is checked. The way I know which checkbox is being checked is by passing a paramater through a command, and that parameter is bound to the "NameAndParent" of the object. All of this works fine.
My problem begins when I give the user the option to save which checkboxes have been checked and I save the "Name" of each object next to the checked box into an XML. As you can see, I am only saving the "Name" of the object, but this name has no hook to the checkbox, So I can't go back and "find" the associated checkbox.
When I give the user the option to load one of these saved files, I want to traverse the tree and check the boxes that were saved. The problem is that the checkboxes DONT HAVE A NAME, OR UID, and I cant assign them one through binding because that is not allowed.
Is there anyway to traverse the tree view and somehow compare the saved name to the name of each element child in the tree, and then check that specific checkbox, Or is this something that has to be programmed in a different way?
Just create a boolean IsChecked property in the class that contains your Data, and bind the CheckBox.IsChecked to that. instead of having to manipulate the view, you can more easily manipulate the data it is bound to, removing the need for fancy Visual-Tree operations, and removing the dependency between your application logic and your UI. This is the most important realization of the Model-View-ViewModel Pattern.
If you do not want to introduce UI-related logic (such as the IsChecked property I mentioned) into your Data Model, you will have to introduce a ViewModel in between the Model and the View.

Binding Text Property of child element of ComboBoxItem

I have a WPF ComboBox with a static list of people with an image and first name. I am trying to bind the selected FirstName to a db table. The way I am doing it now (obviously wrong) I am only binding the object and hence writing:
System.Windows.Controls.ComboBoxItem
to my db table.
How can I bind the FirstName from the selected ComboBoxItem from the TextBlock.Text property? Is there a way to do this purely in WPF?
<ComboBox Text="{Binding Path=FirstName}">
<ComboBoxItem>
<StackPanel Orientation="Horizontal" >
<Image Source="/Images/Alice.png" />
<TextBlock Text="Alice" />
</StackPanel>
</ComboBoxItem>
<ComboBoxItem>
<StackPanel Orientation="Horizontal">
<Image Source="/Images/Bob.png" />
<TextBlock Text="Bob" />
</StackPanel>
</ComboBoxItem>
</ComboBox>
I am saving the changes to the db table on a button click event
private void SaveAndClose_Click(object sender, RoutedEventArgs e)
{
bindingView = (BindingListCollectionView)myCollection.View;
bindingView.CommitEdit();
db.SubmitChanges();
}
Instead of hard-coding the comboboxitems, you should use a datatemplate. Then, you should use the SelectedValuePath to select the value based on the FirstName property in your items. Then you can bind to the textbox.
If you want to bind to the textbox in xaml directly, you'll need to name the element (x:Name="myTextBox"). Then, bind your combobox's SelectedValue like so: SelectedValue="{Binding ElementName="myTextBox", Path="Text"}".
In general, I don't think binding a combobox to a textbox is a very good idea.

Databinding and bound buttons

I have an ItemsControl bound to a list of objects.
It basically displays a list of bound properties on screen with a textbox and button next to each one to allow you to add additional information to each database field.
In the onclick of the button I need to take the string in the textbox and store it in the object for that item. The XAML looks a bit like this
<ItemsControl x:Name="ListDatabaseFields" ItemsSource="{Binding Path=SelectedImport.ColumnMappings}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<TextBlock Text="{Binding DatabaseColumn.Name}" TextWrapping="Wrap" Grid.Column="0"/>
<TextBox Name="txtNewFileField" Width="100"/>
<Button Name="Add" Content="Add File Field" Style="{StaticResource LinkButton}" Width="50" Click="Add_Click"/>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Whats the best way in the XAML to access the ColumnMapping the itemcontrol is bound to for that item and change its file field property.
I'm not sure if i understood your question correctly, please clarify if this is not what you are looking for.
I have adapted this to my test application, my DataTemplate looks like this:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBox MinWidth="100" Name="tbNewName"/>
<Button MinWidth="100"
Tag="{x:Reference tbNewName}" Click="ButtonNewName_Click"
Content="Do Stuff"/>
</StackPanel>
</DataTemplate>
Notice the Button.Tag which references the TextBox, i can use it in the handler like this:
private void ButtonNewName_Click(object sender, RoutedEventArgs e)
{
Employee emp = (sender as FrameworkElement).DataContext as Employee;
TextBox tbNewName = (sender as FrameworkElement).Tag as TextBox;
emp.Name = tbNewName.Text;
}
The DataContext is inherited, so the sender (the Button) contains the data-item, in your case a ColumnMapping, the Tag gives me the TextBox and that is all i need for changing a property.
If you need to reference more controls you could create an array in the Tag.

How to modify silverlight combobox data display

I have a combobox defined as follows:
<ComboBox x:Name="cboDept" Grid.Row="0" Margin="8,8,8,8" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"
ItemsSource="{Binding Source={StaticResource cvsCategories}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Width="Auto" Height="Auto">
<sdk:Label Content="{Binding CategoryID}" Height="20" />
<sdk:Label Content="{Binding CategoryName}" Height="20" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
It works fine. However, once I select an item in the list, I want a different template to be applied to the combobox selected item being shown to the user (the item shown after the disappearance of popup). In the above case, I want only CategoryName to be displayed in the ComboBox once I select the respective item.
Can anyone let me know on how to achieve this?
thanks
What you need to do is create a ResourceDictionary containing a few defined templates yourself. In the below, ComboBoxTemplateOne and ComboBoxTeplateTwo are user controls that are set out to display the combobox in the manor you want.
<UserControl.Resources>
<ResourceDictionary>
<DataTemplate x:Key="TemplateOne">
<local:ComboBoxTemplateOne />
</DataTemplate>
<DataTemplate x:Key="TemplateTwo">
<local:ComboBoxTemplateTwo />
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
You will then need to create your own class that inherits from ContentControl "DataTemplateSelector", overriding OnContentChanged
Protected Overrides Sub OnContentChanged(ByVal oldContent As Object, ByVal newContent As Object)
MyBase.OnContentChanged(oldContent, newContent)
Me.ContentTemplate = SelectTemplate(newContent, Me)
End Sub
You will then need to create another class that inherits from the above DataTemplateSelector which overrides SelectTemplate ("TemplateSelectorClass"), which will return the DataTemplate defined above ("TemplateOne" or "TemplateTwo").
Also in this derived class, you will need to define a property for each of the templates you have
Public Property ComboboxTemplateOne As DataTemplate
Then head back to your XAML and n the blow XAML
<local:TemplateSelectorClass ComboboxTemplateOne="{StaticResource TemplateOne}" Content="{Binding Path=ActiveWorkspace}>
This should work, as it is effectively doing the same work as setting the "DataTemplate" property in WPF (which doesn't exist in SilverLight)
I realise there are a fair few steps here and its quite fiddly, but hopefully this will get you there. Any questions just shout.

Getting the SelectedItem from a combobox in a DataTemplate

Lets say I've got a DataTemplate like so
<DataTemplate x:Key="Body">
<StackPanel Orientation="Horizontal">
<ComboBox ItemsSource="{Binding Path=Person.Children}"></ComboBox>
<Button Click="Button_Click">Hello</Button>
</StackPanel>
</DataTemplate>
Which shows a list of ComboBoxes followed by a button.
Now, on clicking the button I need to discover the value in the combo next to the button pressed. I can get the data context as below but can't work out how to get the combos SelectedItem
private void Button_Click(object sender, RoutedEventArgs e)
{
// Can get the data context
var p = ((Button)sender).DataContext as Person;
// How to get the value in the combo ...?
}
you can also reference the combobox in your codebehind if you give it a name. However its cleaner to use a separate class to do your logic than the code behind. Such as a viewmodel.
then you could also do something like this...
<DataTemplate x:Key="Body">
<StackPanel Orientation="Horizontal">
<ComboBox ItemsSource="{Binding Path=Person.Children}"
SelectedItem="{Binding Path=SelectedChild}"
IsSynchronizedWithCurrentItem="True"/>
<Button Command="{Binding Path=ButtonCommand}">Hello</Button>
</StackPanel>
</DataTemplate>
Instead of using the Click event handler, use a Command and bind the CommandParameter property to the ComboBox.SelectedItem. Then in your command's executed logic, you can use the parameter.

Resources