Assume that you want to limit the rate at which the service host reads messages from a queue. And, when the system is “under stress” you want to stop further message consumption. Read more...
The problem with the IsMouseOver property on TreeViewItem in WPF is that when it is set to true for a child node it is also true for parent nodes. Therefore if you try to add a trigger with IsMouseOver property you will observe the following behavior:
|
Code Snippet |
|
<TreeView x:Name="TreeViewControl" ItemsSource="{Binding}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Item}" ItemsSource="{Binding Path=SubItems}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<Style TargetType="TreeViewItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Pink"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.Resources>
</TreeView> |

One way to solve this is to overwrite the ControlTemplate of TreeViewItem and add a trigger with IsMouseOver propert on PART_Header. In that case trigger will be activated only if mouse is directly over the TreeViewItem, i.e. its header. Creating a ControlTemplate for TreeViewItem from scratch is not straight forward and it might require a lot of work, however you can copy the default control template of TreeViewItem from msdn. Then add MouseOver trigger among ControlTemplate.Triggers list as indicated below. With this change when you move your mouse over a TreeViewItem, parent nodes will not be affected by the trigger.
|
Code Snippet |
|
<Style x:Key="{x:Type TreeViewItem}"
TargetType="{x:Type TreeViewItem}">
…
…
…
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
…
…
<ControlTemplate.Triggers>
<Trigger SourceName="PART_Header" Property="IsMouseOver" Value="True">
<Setter TargetName="Bd" Property="Background" Value="Pink"/>
</Trigger>
<Trigger Property="IsExpanded"
Value="false">
<Setter TargetName="ItemsHost"
Property="Visibility"
Value="Collapsed"/>
</Trigger>
…
…
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> |

It is possible to set the selected item of a TreeView control in WPF without direct interaction with UI elements by including IsSelected property in your data object and binding it to IsSelected property of TreeViewItem. The code snippets below indicate how to set expanded and selected items of a TreeView control by updating the data that is bound to UI element.
- Define the style for TreeViewItem so that IsSelected and IsExpanded properties are bound to corresponding properties on the data:
|
Code Snippet: |
|
<TreeView x:Name="TreeViewControl" ItemsSource="{Binding}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Item}" ItemsSource="{Binding Path=SubItems}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected}"/>
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/>
</Style>
</TreeView.Resources>
</TreeView> |
- Define the Properties in your data class:
|
Code Snippet: |
|
public class Item : INotifyPropertyChanged
{
private string name;
private bool isSelected;
private bool isExpanded;
…
…
public bool IsExpanded
{
get
{
return isExpanded;
}
set
{
isExpanded = value;
this.OnPropertyChanged("IsExpanded");
}
}
public bool IsSelected
{
get
{
return isSelected;
}
set
{
isSelected = value;
this.OnPropertyChanged("IsSelected");
}
}
…
…
} |
- Set the values in the Loaded method so that parent node is expanded and child node is selected when the treeview is displayed.
|
Code Snippet: |
|
void Window1_Loaded(object sender, RoutedEventArgs e)
{
ObservableCollection<Item> items = new ObservableCollection<Item>();
Item item1 = new Item() {Name = "Item1"};
item1.SubItems.Add(new Item() {Name="Item1_1"});
items.Add(item1);
this.TreeViewControl.DataContext = items;
// Expand parent node and select the child node
item1.IsExpanded = true;
item1.SubItems[0].IsSelected = true;
} |

You can have Office-like tooltips for the hyperlinks displayed in RichTextBox control in WPF. First thing to note is enabling the hyperlinks in RichTextBox by setting IsDocumentEnabled property. Then, we will bind the ToolTip property of the hyperlink to its NavigateUri property to be able to use it in the new DataTemplate of the ToolTip. The DataTemplate is defined so that when RichTextBox is editable the tooltip will display “Ctrl + Click to follow link” along with the navigation address, but in read-only mode the helper text will be “Click to follow link”.


|
Code Snippet |
|
<RichTextBox Grid.Column="1" IsDocumentEnabled="True" IsReadOnly="True">
<FlowDocument>
<Paragraph>
<Hyperlink NavigateUri="http://www.msdn.com">
Go to msdn
</Hyperlink>
</Paragraph>
</FlowDocument>
<RichTextBox.Resources>
<Style TargetType="{x:Type Hyperlink}">
<Setter Property="ToolTip" Value="{Binding Path=NavigateUri, RelativeSource={RelativeSource Self}}"/>
</Style>
<Style TargetType="ToolTip">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}"/>
<TextBlock x:Name="MessageTextBlock" FontWeight="Bold" Text="Ctrl + Click to follow link"/>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsReadOnly, RelativeSource={RelativeSource AncestorType={x:Type RichTextBox}}}" Value="True">
<Setter TargetName="MessageTextBlock" Property="Text" Value="Click to follow link"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</RichTextBox.Resources>
</RichTextBox> |
If you have trouble loading your Outlook add-in, first make sure it is listed in the trust center. To do this open the dialog Tools->Trust Center->Add-ins.
If your add-in is listed under “Inactive Application Add-ins” this means it could not be loaded and in fact if you select COM Add-ins to display COM Add-ins dialog and select your add-in from the list, more likely you will see the following message: “Not loaded. A runtime error occurred during the loading of the COM Add-in." This message does not say much however you can get a more detailed message as explained @ msdn:
“Visual Studio Tools for Office can write all errors that occur during startup to a log file or display each error in a message box. By default, these options are turned off for application-level projects. You can turn the options on by adding and setting environment variables. To display each error in a message box, set the VSTO_SUPPRESSDISPLAYALERTS variable to 0 (zero). You can suppress the messages by setting the variable to 1 (one). To write the errors to a log file, set the VSTO_LOGALERTS variable to 1 (one). Visual Studio Tools for Office creates the log file in the folder that contains the application manifest. The default name is <Manifestname>.manifest.log. To stop logging errors, set the variable to 0 (zero). For information about setting environment variables in Microsoft Windows XP, see "How To Manage Environment Variables in Windows XP" (http://support.microsoft.com/default.aspx?scid=kb;en-us;310519).”
Full article is at: <http://msdn2.microsoft.com/en-us/library/ms269003(VS.80).aspx>
If your add-in is listed under “Disabled Application add-ins” first enable it by opening Disabled Items dialog and follow the steps explained above to troubleshoot the error.
