Programatically set the ToggleButton style within a treeviewitem - wpf

Does anyone know a way to set the style of a ToggleButton within a treeview item please?
As in something like treeviewItem.ToggleButton.Style = "Blah" if you see what I mean.
Many thanks.

should be like this.
mytogglebutton.Style = CType(TryFindResource("MyToggleButtonStyle"),Windows.Style)

Related

Set VisualState of combobox when a item is selected in Xaml (Silverlight)

When my combobox expands and I select an item, I want the combobox to change visual state(it is highlighted). This will signify something is selected. I tried various VisualStates but none of them would trigger in this scenario. How can I achieve this? Thanks.
The standard ComboBox simply doesn't have states to distinguish between having something selected and having nothing selected.
There are a number of ways to go about solving the underlying problem, and it depends mostly on the answer to the following question:
Do you really need to change the visual appearance of the ComboBox itself or does it suffice to style the selected item more prominently?
If it's the latter, you're best served with the rather easy way of using a custom control template for the ComboBoxItems.
If you really want to style the ComboBox itself that way, there are two options I can think of:
A) Add custom states to a ComboBox with a custom template.
Copy your ComboBox's control template and add another state group to the already present states. Both of this is typically done in Expression Blend.
After that you can update the new states in code with
VisualStateManager.GoToState(this, "Selected", true);
for example. You will have to set those states yourself when the first item is chosen. This could be done on the SelectionChanged event.
B) Derive from ComboBox
If you want to use the control in this way often, it might be worthwhile to derive from ComboBox to make your own custom control.
It would look somthing like this:
[TemplateVisualState(Name = "SelectedStates", GroupName = "Unselected")]
[TemplateVisualState(Name = "SelectedStates", GroupName = "Selected")]
// ... (more attributes copied from the ComboBox ones)
public class MyComboBox : ComboBox
{
public MyComboBox()
{
SelectionChanged += HandleSelectionChanged;
DefaultStyleKey = typeof(MyComboBox);
}
void HandleSelectionChanged(object sender, SelectionChangedEventArgs e)
{
VisualStateManager.GoToState(this, SelectedItem != null ? "Selected" : "Unselected", true);
}
}
And you would then need a default style based on the default ComboBox style (or whatever you usually use).
Note that I didn't test this in any way.

Getting ListBox Items Silverlight

I have tried to get the listbox items using VisualTreeHelper class. When I do VisualTreeHelper.GetChildrenCount((DependencyProperty)listBox1) it returns count as 0. But the listbox has lot of listboxitems in it.Can someone let me know if I am doing any mistake?
Regards,
Lalith
Have you tried to use the ItemContainerGenerator property of the Listbox class ? It has some methods you can use to retrieve Listbox items.
http://msdn.microsoft.com/en-US/library/system.windows.controls.itemscontrol.itemcontainergenerator(v=VS.95).aspx

Accessibility for Silverlight Combobox Items

In my Silverlight page I have a combobox. In the code-behind I'm filling the combobox items like so:
this.ProblemList.Items.Add(Strings.Review_SelectProblem);
this.ProblemList.Items.Add(Strings.Review_IncorrectCharacters);
this.ProblemList.Items.Add(Strings.Review_MissingText);
...
this.ProblemList.SelectedIndex = 0; //Set the default selection
Elsewhere, in my XAML page I am providing accessibility (for the disabled) to other non-combobox controls by doing this:
AutomationProperties.Name="{Binding Strings.Review_Access_ParagraphCorrect}"
I'd like to provide Accessibility to my combobox items but the only way I've been able to find is like so:
AutomationProperties.SetLabeledBy(this.nameInput, this.nameLabel);
The problem with this is that my combobox items must have a name. How do I assign a name to my combobox items programmaticly or how can I provide accessibility in the code behind with out referencing the name of the combobox items?
Thank you for your help,
Aaron
You can try to use something like this:
ComboBoxItem tmpItem = new ComboBoxItem();
tmpItem.Content = Strings.Review_SelectProblem;
tmpItem.Name = Strings.Review_SelectProblem;
this.ProblemList.Items.Add(tmpItem);
I hope I understood you correctly.

How do I disable the opening and closing animation of the DropDown of a ComboBox in WPF?

When I put a combobox in my WPF app, at runtime, when I click it, it rolls down the included items. After selection, it rolls the DropDown up again.
Is it possible to prevent the rolling animation from happening? Instead I'ld like to just have it open and closed immediately.
-
Marc
I don't think it's possible to simply disable the animation on the ComboBox as it stands. However, I believe the default ControlTemplate for the ComboBox implements the dropdown portion as a Popup. I'm guessing that it's using the Slide PopupAnimation setting. If you're up for it, you can replace the ControlTemplate for the ComboBox and set the PopupAnimation on that Popup to None or whatever setting you'd like.
Here's an example ComboBox ControlTemplate thanks to Microsoft.
Let me know if you need further assistance.
--
HTH,
Dusty
You can also create a custom ComboBox ala this answer; so you'd end up with something like this.
class ComboBoxNoAnimation : ComboBox
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var popup = (Popup)Template.FindName("PART_Popup", this);
popup.PopupAnimation = PopupAnimation.None;
}
}

Binding to properties of Storyboard declared in UserControl

I'm struggling with a binding that only works when declared inside a Window's resources area. As soon as I move the declaration to a UserControl's resources area, the binding fails. No error message, but the value is not updated when the value of the slider (source) is changed. I would like to use the storyboard inside one of the UserControl's VSM states.
Can someone please tell me why this happens and hopefully how to fix it?
I tried pasting my xaml here, but the website doesn't seam to allow the xml tags in the message.
Regards
Jaco
Only set the Storyboard.TargetProperty (and not .Target or .TargetName) and point the storyboard to the object in code:
Dim SB as Storyboard = Me.FindResource("Storyboard_name_goes_here")
TargetObject.BeginStoryboard(SB, HandoffBehavior.Compose)
Hope this helps...

Resources