<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->
<UserControl x:Class="Microsoft.Windows.Controls.Samples.CustomEvents"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls">
<StackPanel>
<ContentControl Content="Populating and Populated events" Style="{StaticResource Header}" />
<StackPanel>
<TextBlock Style="{StaticResource Information}">
The AutoCompleteBox control is able to work with unbound data as
well. By subscribing to the Populating event of an
AutoCompleteBox control, as a developer you can cancel the
population, change ItemsSource data, or otherwise react to the
input before the control provides suggestions.
</TextBlock>
</StackPanel>
<ContentControl Content="Text completion off" Style="{StaticResource Header}" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Padding="5">Immediate population:</TextBlock>
<controls:AutoCompleteBox
x:Name="NowAutoComplete"
SearchMode="None"
Width="300"
IsTextCompletionEnabled="False"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="0, 0, 0, 12"
Tag="Value1"
SelectionChanged="OnSelectionChanged"
/>
<ContentPresenter x:Name="Value1" Grid.Column="2" />
<TextBlock Padding="5" Grid.Row="1">Delayed population:</TextBlock>
<controls:AutoCompleteBox
x:Name="LaterAutoComplete"
SearchMode="None"
IsTextCompletionEnabled="False"
Width="300"
Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Left"
Tag="Value2"
SelectionChanged="OnSelectionChanged"
/>
<ContentPresenter x:Name="Value2" Grid.Row="1" Grid.Column="2" />
</Grid>
<ContentControl Content="Text completion on" Style="{StaticResource Header}" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Padding="5">Immediate population:</TextBlock>
<controls:AutoCompleteBox
x:Name="NowAutoComplete2"
SearchMode="None"
IsTextCompletionEnabled="True"
Width="300"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="0, 0, 0, 12"
Tag="Value3"
SelectionChanged="OnSelectionChanged"
/>
<ContentPresenter x:Name="Value3" Grid.Column="2"/>
<TextBlock Padding="5" Grid.Row="1">Delayed population:</TextBlock>
<controls:AutoCompleteBox
x:Name="LaterAutoComplete2"
SearchMode="None"
IsTextCompletionEnabled="True"
Width="300"
Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Left"
Tag="Value4"
SelectionChanged="OnSelectionChanged"
/>
<ContentPresenter x:Name="Value4" Grid.Row="1" Grid.Column="2"/>
</Grid>
<src:SourceViewer xmlns:src="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<src:SourceFile Path="CustomEvents.xaml">
<src:SourceFile.Source>
<sys:String><!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->
<UserControl x:Class="Microsoft.Windows.Controls.Samples.CustomEvents"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls">
<StackPanel>
<ContentControl Content="Populating and Populated events" Style="{StaticResource Header}" />
<StackPanel>
<TextBlock Style="{StaticResource Information}">
The AutoCompleteBox control is able to work with unbound data as
well. By subscribing to the Populating event of an
AutoCompleteBox control, as a developer you can cancel the
population, change ItemsSource data, or otherwise react to the
input before the control provides suggestions.
</TextBlock>
</StackPanel>
<ContentControl Content="Text completion off" Style="{StaticResource Header}" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Padding="5">Immediate population:</TextBlock>
<controls:AutoCompleteBox
x:Name="NowAutoComplete"
SearchMode="None"
Width="300"
IsTextCompletionEnabled="False"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="0, 0, 0, 12"
Tag="Value1"
SelectionChanged="OnSelectionChanged"
/>
<ContentPresenter x:Name="Value1" Grid.Column="2" />
<TextBlock Padding="5" Grid.Row="1">Delayed population:</TextBlock>
<controls:AutoCompleteBox
x:Name="LaterAutoComplete"
SearchMode="None"
IsTextCompletionEnabled="False"
Width="300"
Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Left"
Tag="Value2"
SelectionChanged="OnSelectionChanged"
/>
<ContentPresenter x:Name="Value2" Grid.Row="1" Grid.Column="2" />
</Grid>
<ContentControl Content="Text completion on" Style="{StaticResource Header}" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Padding="5">Immediate population:</TextBlock>
<controls:AutoCompleteBox
x:Name="NowAutoComplete2"
SearchMode="None"
IsTextCompletionEnabled="True"
Width="300"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="0, 0, 0, 12"
Tag="Value3"
SelectionChanged="OnSelectionChanged"
/>
<ContentPresenter x:Name="Value3" Grid.Column="2"/>
<TextBlock Padding="5" Grid.Row="1">Delayed population:</TextBlock>
<controls:AutoCompleteBox
x:Name="LaterAutoComplete2"
SearchMode="None"
IsTextCompletionEnabled="True"
Width="300"
Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Left"
Tag="Value4"
SelectionChanged="OnSelectionChanged"
/>
<ContentPresenter x:Name="Value4" Grid.Row="1" Grid.Column="2"/>
</Grid>
</StackPanel>
</UserControl>
</sys:String>
</src:SourceFile.Source>
</src:SourceFile>
<src:SourceFile Path="CustomEvents.xaml.cs">
<src:SourceFile.Source>
<sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
// Global suppressions for this sample
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "Microsoft.Windows.Controls.Samples.CustomEvents.#Value1")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "Microsoft.Windows.Controls.Samples.CustomEvents.#Value2")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "Microsoft.Windows.Controls.Samples.CustomEvents.#Value3")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "Microsoft.Windows.Controls.Samples.CustomEvents.#Value4")]
namespace Microsoft.Windows.Controls.Samples
{
/// <summary>
/// The PopulationEvents class shows how a developer might hook into the
/// population events to provide custom data.
/// </summary>
[Sample("(2)Custom Events", DifficultyLevel.Basic)]
[Category("AutoCompleteBox")]
public partial class CustomEvents : UserControl
{
/// <summary>
/// Initializes a new instance of the type.
/// </summary>
public CustomEvents()
{
InitializeComponent();
Loaded += OnLoaded;
}
/// <summary>
/// Handle the Loaded event of the page.
/// </summary>
/// <param name="sender">The source object.</param>
/// <param name="e">The event arguments.</param>
private void OnLoaded(object sender, RoutedEventArgs e)
{
NowAutoComplete.Populating += OnPopulatingSynchronous;
NowAutoComplete2.Populating += OnPopulatingSynchronous;
LaterAutoComplete.Populating += OnPopulatingAsynchronous;
LaterAutoComplete2.Populating += OnPopulatingAsynchronous;
}
/// <summary>
/// The Populating event handler.
/// </summary>
/// <param name="sender">The source object.</param>
/// <param name="e">The event data.</param>
private void OnPopulatingSynchronous(object sender, PopulatingEventArgs e)
{
AutoCompleteBox source = (AutoCompleteBox)sender;
source.ItemsSource = new string[]
{
e.Parameter + "1",
e.Parameter + "2",
e.Parameter + "3",
};
}
/// <summary>
/// The populating handler.
/// </summary>
/// <param name="sender">The source object.</param>
/// <param name="e">The event data.</param>
private void OnPopulatingAsynchronous(object sender, PopulatingEventArgs e)
{
AutoCompleteBox source = (AutoCompleteBox)sender;
// Cancel the populating value: this will allow us to call
// PopulateComplete as necessary.
e.Cancel = true;
// Use the dispatcher to simulate an asynchronous callback when
// data becomes available
Dispatcher.BeginInvoke(
delegate
{
source.ItemsSource = new string[]
{
e.Parameter + "1",
e.Parameter + "2",
e.Parameter + "3",
};
// Population is complete
source.PopulateComplete();
});
}
/// <summary>
/// Called when an AutoCompleteBox's selected value changes. Uses the
/// Tag property to identify the content presenter to be updated.
/// </summary>
/// <param name="sender">The source AutoCompleteBox control.</param>
/// <param name="e">The event data.</param>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler is wired up in XAML.")]
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
AutoCompleteBox acb = (AutoCompleteBox)sender;
// In these sample scenarios, the Tag is the name of the content
// presenter to use to display the value.
string contentPresenterName = (string)acb.Tag;
ContentPresenter cp = FindName(contentPresenterName) as ContentPresenter;
if (cp != null)
{
cp.Content = acb.SelectedItem;
}
}
}
}</sys:String>
</src:SourceFile.Source>
</src:SourceFile>
</src:SourceViewer>
</StackPanel>
</UserControl>