with a winrt user control how do i dynamically create controls based on dependency property - user-controls

I'm trying to create a user control that will display a collection of images. I have the user control created with a dependency property for the collection of images. I can databind to that property and display the images fine.
However, I have hard-coded in the size of the collection and I want it to take in an arbitrary number of images and display them.
I am not sure how to dynamically create the images based on the collection of images. I'm sure there is some event that I want to respond to and create my images based on the contents of the dependency property.
public sealed partial class ImageView : UserControl
{
public ImageView()
{
this.InitializeComponent();
scrollViewer.ZoomSnapPoints.Clear();
scrollViewer.ZoomSnapPoints.Add(0.2f);
scrollViewer.ZoomSnapPoints.Add(0.6f);
scrollViewer.ZoomSnapPoints.Add(1.0f);
scrollViewer.ZoomSnapPoints.Add(1.4f);
scrollViewer.ZoomToFactor(0.4f);
}
public static readonly DependencyProperty ImagesProperty = DependencyProperty.Register("Images",
typeof(List<VMImage>), typeof(ImageView), new PropertyMetadata(new List<VMImage>(12)));
public List<VMImage> Images
{
get { return (List<VMImage>)GetValue(ImagesProperty); }
set { SetValue(ImagesProperty, value); }
}
}
<ScrollViewer Height="700" Width="700"
x:Name="scrollViewer"
MinZoomFactor="0.2"
MaxZoomFactor="5.0"
ZoomSnapPointsType="Mandatory">
<Canvas Background="Black" Width="2000" Height="2000" >
<Image Canvas.Left="{Binding Images[0].Location.X}"
Canvas.Top="{Binding Images[0].Location.Y}"
Source="{Binding Images[0].Source}" ></Image>
<Image Canvas.Left="{Binding Images[1].Location.X}"
Canvas.Top="{Binding Images[1].Location.Y}"
Source="{Binding Images[1].Source}" ></Image>
</Canvas>
</ScrollViewer>

You should make your dependency property an ObservableCollection<VMImage>. This way you can attach a handler to its CollectionChanged event and get notified of changes. Just make sure in the property changed callback of your dependency property that you add the event handler to the new value and remove it from the old value.
public static readonly DependencyProperty ImagesProperty = DependencyProperty.Register("Images",
typeof(ObservableCollection<VMImage>), typeof(ImageView), new PropertyMetadata(null, OnImagesChanged));
public ObservableCollection<VMImage> Images
{
get { return (ObservableCollection<VMImage>)GetValue(ImagesProperty); }
set { SetValue(ImagesProperty, value); }
}
private static void OnImagesChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
// use wrapper to pass DependencyObject to handler
NotifyCollectionChangedEventHandler handler = (s, args) => OnCollectionChanged(target, args);
if (e.OldValue is ObservableCollection<object>)
(e.OldValue as ObservableCollection<object>).CollectionChanged -= handler;
if (e.NewValue is ObservableCollection<object>)
(e.NewValue as ObservableCollection<object>).CollectionChanged += handler;
var imageView = target as ImageView
if (imageView != null)
{
// collection has changed completely, replace all images
}
}
static void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
var imageView = sender as ImageView;
if (imageView != null)
{
if (e.OldItems != null)
{
// remove images
}
if (e.NewItems != null)
{
// add images
}
}
}

Related

How to send messages between threads using CommunityToolkit.Mvvm.Messaging and WinUI 3?

The following simple multi-threaded program was meant to try out the CommunityToolkit Messenger package for which the documentation says (see: Messenger)
Both WeakReferenceMessenger and StrongReferenceMessenger also expose a Default property that offers a thread-safe implementation built-in into the package.
I had hoped this would mean I could send messages on one thread and receive them on other threads but a problem arose with what seems to be the IMessenger Interface. Details follow below.
This project starts with a vanilla TemplateStudio WinUI 3 (v1.1.5) desktop template that uses the CommunityToolkit Mvvm package (with Messenger) and a single page, MainPage. When the App launches, it starts a RandomMessageGenerator thread that periodically issues a TraceMessage using the WeakReferenceMessenger.Default channel from the Toolkit. The UI thread receives these messages and stores them in a List.
App.xaml.cs
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml;
using Multi_Window.Activation;
using Multi_Window.Contracts.Services;
using Multi_Window.Core.Contracts.Services;
using Multi_Window.Core.Services;
using Multi_Window.Services;
using Multi_Window.ViewModels;
using Multi_Window.Views;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using CommunityToolkit.Mvvm.Messaging.Messages;
using System.Diagnostics;
namespace Multi_Window;
// To learn more about WinUI 3, see https://docs.microsoft.com/windows/apps/winui/winui3/.
public partial class App : Application
{
// The .NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
// https://docs.microsoft.com/dotnet/core/extensions/configuration
// https://docs.microsoft.com/dotnet/core/extensions/logging
public IHost Host { get; }
public static T GetService<T>()
where T : class
{
if ((App.Current as App)!.Host.Services.GetService(typeof(T)) is not T service)
{
throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs.");
}
return service;
}
public static WindowEx MainWindow { get; } = new MainWindow();
public static ShellPage? ShellPage { get; set; }
private static readonly List<string> _traceMessages = new();
private Task? messageGenerator;
public App()
{
InitializeComponent();
Host = Microsoft.Extensions.Hosting.Host.
CreateDefaultBuilder().
UseContentRoot(AppContext.BaseDirectory).
ConfigureServices((context, services) =>
{
// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();
// Other Activation Handlers
// Services
services.AddTransient<INavigationViewService, NavigationViewService>();
services.AddSingleton<IActivationService, ActivationService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
// Core Services
services.AddSingleton<IFileService, FileService>();
// Views and ViewModels
services.AddTransient<MainViewModel>();
services.AddTransient<MainPage>();
// ** NOTE ** changed to Singleton so we can refer to THE ShellPage/ShellViewModel
services.AddSingleton<ShellPage>();
services.AddSingleton<ShellViewModel>();
// Configuration
}).
Build();
UnhandledException += App_UnhandledException;
System.AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Microsoft.UI.Xaml.Application.Current.UnhandledException += Current_UnhandledException;
}
private void RandomMessageGenerator()
{
var shutdown = false;
WeakReferenceMessenger.Default.Register<ShutDownMessage>(this, (r, m) => shutdown = true);
Debug.WriteLine($"RandomMessageGenerator started on thread {Environment.CurrentManagedThreadId}");
Random rnd = new();
// not a good way to control thread shutdown in general but will do for a quick test
while (shutdown == false)
{
Thread.Sleep(rnd.Next(5000));
var tm = new TraceMessage($"{DateTime.Now:hh:mm:ss.ffff} Timer event. (Th: {Environment.CurrentManagedThreadId})");
try
{
WeakReferenceMessenger.Default.Send(tm);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
break;
}
}
Debug.WriteLine($"RandomMessageGenerator closed at {DateTime.Now:hh:mm:ss.ffff} (Th: {Environment.CurrentManagedThreadId})");
}
private void Current_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) => throw new NotImplementedException();
private void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e) => throw new NotImplementedException();
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
// TODO: Log and handle exceptions as appropriate.
// https://docs.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.unhandledexception.
throw new NotImplementedException();
}
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);
await App.GetService<IActivationService>().ActivateAsync(args);
MainWindow.AppWindow.Closing += OnAppWindowClosing;
WeakReferenceMessenger.Default.Register<TraceMessage>(this, (r, m) =>
{
_traceMessages.Add(m.Value);
Debug.WriteLine(m.Value);
});
WeakReferenceMessenger.Default.Register<WindowClosedMessage>(this, (r, m) => OnStatusWindowClosed()); // StatusWindow closed events
WeakReferenceMessenger.Default.Register<App, TraceMessagesRequest>(this, (r, m) => m.Reply(_traceMessages)); // StatusWindow requests previous messages
messageGenerator = Task.Run(RandomMessageGenerator);
}
private void OnStatusWindowClosed()
{
if (ShellPage is not null && ShellPage.SettingsStatusWindow)
{
ShellPage.SettingsStatusWindow = false; // turn off toggle
if (ShellPage.NavigationFrame.Content is MainPage settingsPage) settingsPage.StatusWindowToggle.IsOn = false;
}
}
private async void OnAppWindowClosing(object sender, AppWindowClosingEventArgs e)
{
WeakReferenceMessenger.Default.UnregisterAll(this); // stop messages and avoid memory leaks
WeakReferenceMessenger.Default.Send(new ShutDownMessage(true)); // close all windows
MainWindow.AppWindow.Closing -= OnAppWindowClosing;
if (messageGenerator is not null) await messageGenerator;
}
}
The user may create a StatusWindow (a secondary Window on the UI thread) by toggling a switch on MainPage. The StatusWindow should open, request and load previous messages from the App, then register for new TraceMessages. All TraceMessages (including new ones) are displayed in a ListView on the StatusWindow.
MainPage.xaml.cs
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Multi_Window.ViewModels;
namespace Multi_Window.Views;
public sealed partial class MainPage : Page
{
public MainViewModel ViewModel { get; } = App.GetService<MainViewModel>();
public ShellPage ShellPage { get; } = App.GetService<ShellPage>();
public ShellViewModel ShellViewModel { get; } = App.GetService<ShellViewModel>();
public MainPage()
{
DataContext = ViewModel;
InitializeComponent();
}
private void StatusWindow_Toggled(object sender, RoutedEventArgs e)
{
if (StatusWindowToggle.IsOn && ShellPage.SettingsStatusWindow == false)
{
StatusWindow window = new() { Title = "Prosper Status" };
window.Activate();
ShellPage.SettingsStatusWindow = true;
}
else if (StatusWindowToggle.IsOn == false && ShellPage.SettingsStatusWindow == true)
WeakReferenceMessenger.Default.Send(new CloseWindowMessage(true));
}
}
MainPage.xaml
<Page
x:Class="Multi_Window.Views.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="ContentArea">
<ToggleSwitch x:Name="StatusWindowToggle" x:FieldModifier="public" Grid.Row="2" Grid.Column="1" Header="Show Status Window"
Toggled="StatusWindow_Toggled" IsOn="{x:Bind ShellPage.SettingsStatusWindow, Mode=OneTime}" />
</Grid>
</Page>
StatusWindow.xaml.cs
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace Multi_Window.Views;
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class StatusWindow : Window
{
private ObservableCollection<string> _traceMessages { get; } = new();
public StatusWindow()
{
InitializeComponent();
var sw = new WindowPrimitives(this);
sw.AppWindow.SetIcon("Assets/wip.ico");
WeakReferenceMessenger.Default.Register<CloseWindowMessage>(this, (r, m) => Close());
WeakReferenceMessenger.Default.Register<ShutDownMessage>(this, (r, m) => Close());
}
private void StatusWindow_Closed(object sender, WindowEventArgs args)
{
WeakReferenceMessenger.Default.UnregisterAll(this); // stop getting messages and avoid memory leaks
WeakReferenceMessenger.Default.Send(new WindowClosedMessage(true)); // acknowledge closure
}
private void StatusMessages_Loaded(object sender, RoutedEventArgs e)
{
// get current Trace messages
var messages = WeakReferenceMessenger.Default.Send<TraceMessagesRequest>();
if (messages != null && messages.Responses.Count > 0)
foreach (var response in messages.Responses)
foreach (var trace in response)
_traceMessages.Add(trace);
// register for Trace messages and, when they arrive, add them to list
WeakReferenceMessenger.Default.Register<TraceMessage>(this, (r, m) => _traceMessages.Add(m.Value));
}
}
StatusPage.xaml
<Window
x:Class="Multi_Window.Views.StatusWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Closed="StatusWindow_Closed"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListView x:Name="StatusMessages" x:FieldModifier="public" VerticalAlignment="Top" Margin="20" SelectionMode="None" BorderBrush="Black" BorderThickness="1"
ItemsSource="{x:Bind _traceMessages, Mode=OneWay}"
ScrollViewer.HorizontalScrollMode="Enabled"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.IsHorizontalRailEnabled="True"
ScrollViewer.IsDeferredScrollingEnabled="False"
Loaded="StatusMessages_Loaded">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel VerticalAlignment="Bottom" ItemsUpdatingScrollMode="KeepLastItemInView"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
</Window>
Other Sundry Classes
// Allows Win32 access to a Window through WinAPI
public class WindowPrimitives
{
public IntPtr HWnd { get; }
private WindowId WindowId { get; }
public AppWindow AppWindow { get; }
public Window Window { get; }
public WindowPrimitives(Window window)
{
HWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
WindowId = Win32Interop.GetWindowIdFromWindow(HWnd);
AppWindow = AppWindow.GetFromWindowId(WindowId);
Window = window;
}
}
// Message Definitions
public class CloseWindowMessage : ValueChangedMessage<bool>
{
public CloseWindowMessage(bool value) : base(value) { }
}
public class WindowClosedMessage : ValueChangedMessage<bool>
{
public WindowClosedMessage(bool value) : base(value) { }
}
public class ShutDownMessage : ValueChangedMessage<bool>
{
public ShutDownMessage(bool value) : base(value) { }
}
public class TraceMessage : ValueChangedMessage<string>
{
public TraceMessage(string value) : base(value) { }
}
public class TraceMessagesRequest : CollectionRequestMessage<List<string>>
{
}
The problem is that, on the first new TraceMessage sent after the StatusWindow has been opened, the same WeakReferenceMessenger.Default.Send() method that has been happily sending messages between the RandomMessageGenerator thread and the UI thread throws a "The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))" exception and the RandomMessageGenerator thread dies.
The exception is thrown by the WeakReferenceMessenger.Default.Send(tm); statement in the RandomMessageGenerator() method. I assume the issue is in the IMessenger interface (the only interface involved here). Briefly, as I understand it, this interface builds a table of subscribed receivers for each message type. Each receiver is then signaled on each Send().
One possibility is that references in the receiver list are all assumed to marshal to the same thread. If that were so, none of the messages between threads would work but they do before the StatusWindow is opened so that's unlikely. Changing the list of receivers is an obvious place where threading issues might occur. As WeakReferenceMessenger.Default is thread-safe, I thought adding (and deleting) registered receivers would be thread-safe but doesn't seem to be the case here. Finally, it could be the message itself (a string in this case) that is at fault. I don't know for sure but assumed that the Send method took a private copy of the message to deliver to the marshaled thread.
Could any of you please help me understand the mistake I've made here?
I did find a solution for this particular issue. As expected, it's because an object is accessed from a thread other than the thread on which it was created.
To fix the error, add
public static DispatcherQueue UIDispatcherQueue = DispatcherQueue.GetForCurrentThread();
to the App class which will allow any thread access to the UI thread DispatcherQueue. Then, change the message registration in StatusWindow.xaml.cs to
// register for Trace messages and, when they arrive, add them to list
WeakReferenceMessenger.Default.Register<TraceMessage>(this, (r, m) => App.UIDispatcherQueue.TryEnqueue(() => _traceMessages.Add(m.Value)));
This will now marshal the _traceMessages.Add call in the message handler to the UI thread, which is where
private ObservableCollection<string> _traceMessages { get; } = new();
was constructed.
This was easy enough to figure out once I realized that the point where the exception was thrown and the exception message were both rather deceptive. Although the sending of the message is the cause of the message being received, it's the attempt to handle the message on the wrong thread that really throws the exception.
At any rate, this appears to show that the message receiver handler executes on the same thread as the sender. I was hoping that "thread-safe" in the documentation meant messages were automatically marshalled to the receiver's thread.

Resizing the image while uploading image in Item page

Whenever Image is uploaded to the Stock item page, I have to resize the image to thump size and upload the copy of the same.
How do I override the upload function to add my logic?
The upload function is HandleUpload of the PXImageUploader control in PX.Web.UI.
You can try to overwrite that function and then replace the control on the page with yours.
Another way will be to handle the resizing inside the InventoryItemMaint graph.
You can check the ImageUrl field update and do the resize there. Any time you upload a new picture the URL is basically updated. Please don't use the example below in production as it was never fully tested.
// Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod extension should be constantly active
public class InventoryItemMaintExt : PXGraphExtension<InventoryItemMaint>
{
public PXSelect<UploadFileRevision> uploadFileRevisions;
protected virtual void InventoryItem_ImageUrl_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e, PXFieldUpdated baseMethod)
{
baseMethod?.Invoke(sender, e);
if(e.Row is InventoryItem row)
{
if ((string)e.OldValue != row.ImageUrl) //ADD conditions so that this doesn't work any time user change the image if there are multiple attached
{
UpdateImageFileRevisionToResizedImage(sender, row);
}
}
}
private void UpdateImageFileRevisionToResizedImage(PXCache sender, InventoryItem row)
{
var fileNotes = PXNoteAttribute.GetFileNotes(sender, row);
UploadFileRevision uploadedFile = GetFile(sender.Graph, fileNotes, row.ImageUrl);
if (uploadedFile != null)
{
var data = ResizeImage(uploadedFile.Data);
uploadedFile.Data = data;
uploadFileRevisions.Update(uploadedFile);
}
}
//WARNING: DON'T USE THIS METHOD IN PRODUCTION.
// USE ANY OTHER RECOMMENDED METHOD TO RESIZE IMAGES
private static byte[] ResizeImage(byte[] data)
{
System.IO.MemoryStream myMemStream = new System.IO.MemoryStream(data);
System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(myMemStream);
System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(200, 200, null, IntPtr.Zero);
System.IO.MemoryStream myResult = new System.IO.MemoryStream();
newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Jpeg); //Or whatever format you want.
return myResult.ToArray(); //Returns a new byte array.
}
private static UploadFileRevision GetFile(PXGraph graph, Guid[] fileIds,string fileUrl)
{
return (UploadFileRevision)PXSelectBase<UploadFileRevision,
PXSelectJoin< UploadFileRevision,
InnerJoin <UploadFile,
On<UploadFile.fileID, Equal<UploadFileRevision.fileID>,
And<UploadFile.lastRevisionID, Equal<UploadFileRevision.fileRevisionID>>>>,
Where<UploadFile.fileID, In<Required<UploadFile.fileID>>,
And<UploadFile.name,Equal<Required<UploadFile.name>>>>>.Config>.Select(graph, new object[]
{
fileIds,
fileUrl
});
}
}

Cannot capture javafx CheckBoxTableCell CellEditEvent

I have defined a CheckBoc TableColumn as
#FXML private TableColumn<Batch, Boolean> sltd;
And have defined the CellValueFactory & CellFactory
sltd.setCellValueFactory(new PropertyValueFactory<Batch, Boolean>("pr"));
sltd.setCellFactory(CheckBoxTableCell.forTableColumn(sltd));
My problem is i am not able to capture the edit column event for the checkbox. I use the following code:
sltd.setOnEditStart(new EventHandler<TableColumn.CellEditEvent<Batch, Boolean>>() {
#Override
public void handle(TableColumn.CellEditEvent<Batch, Boolean> t) {
//System.out.println("CheckBox clicked.");
}
});
I don't think the check boxes in the CheckBoxTableCell invoke the startEdit(...) method on the table.
The only thing that can happen in an edit is that the boolean property of one of the items in the table changes from true to false, or vice versa. So you can check for this just by listening directly to those boolean properties.
If you want a single listener that will catch changes to any of the properties, you can create an observableList with an "extractor" and register a list change listener with the list. This looks like:
ObservableList<Batch> items = FXCollections.observableArrayList(new Callback<Batch, Observable[]>() {
#Override
public Observable[] call(Batch batch) {
return new Observable[] { batch.prProperty() } ;
}
}
// populate items
table.setItems(items);
items.addListener(new ListChangeListener<Batch>() {
#Override
public void onChanged(Change<? extends Batch> change) {
while (change.hasNext()) {
if (change.wasUpdated()) {
System.out.println("Item at "+change.getFrom()+" changed value");
}
}
}
});

Keeping grid data in a usercontrol by retrieving the criteria from the parent

I have a usercontrol that I want to have the grid inside so I don't have to duplicate that grid on every page. Except when I sort, page, or anything that does a post back the usercontrol reloads and loses its datasource. My plan is to retrieve the search criteria from the parent page(since it already has it from the criteria controls). That way when the NeedDataSource is called it still has the criteria to pass back the right results.
How do I get where you see SuperSearch to be whichever page might be the parent like StateToState.
public SearchCriteria SearchCriteria
{
get
{
Page parent = this.Page;
if (parent != null)
{
var superSearch = parent as SuperSearch;
if (superSearch != null) return superSearch.SearchCriteria;
}
return new SearchCriteria();
}
}
Create an event handler 'event EventHandler NeedSearchCriteria' on your usercontrol that gets fired on your parent page
On your aspx page:
<UC:Grid runat="server" ID="ucGrid" OnNeedSearchCriteria="ucGrid_OnNeedSearchCriteria" />
In the code behind:
public void ucGrid_OnNeedSearchCriteria(object sender, EventArgs e)
{
ucGrid.Criteria = Criteria;
}
And on the usercontrol code behind:
public event EventHandler NeedSearchCriteria;
private SearchCriteria _criteria;
public SearchCriteria Criteria
{
get
{
if (_criteria == null && NeedSearchCriteria != null)
{
NeedSearchCriteria(this, new EventArgs());
}
return _criteria ?? new SearchCriteria();
}
set
{
_criteria = value;
}
}

Preventing TabControl selection in Silverlight

Is there any way to prevent the change of a tab in TabControl in Silverlight 4?
A simple case is when I've got a form with some data, and I want to ask the user if he/she wants to save this data before actually changing the tab.
I've seen code examples on how to do this in WPF, but not in Silverlight.
What can I do to stop the tab from changing?
Bind SelectedIndex to a property on your data context.
<sdk:TabControl SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}">
<sdk:TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</sdk:TabItem>
<sdk:TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</sdk:TabItem>
</sdk:TabControl>
In the SET accessor, write your code to check whether the user really wants to do what they're trying to do.
public class Context : INotifyPropertyChanged
{
int _SelectedIndex = 0;
public int SelectedIndex
{
get
{
return _SelectedIndex;
}
set
{
MessageBoxResult result = MessageBox.Show("Do you want to save?", "Really?", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
_SelectedIndex = value;
}
RaisePropertyChanged("SelectedIndex");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
Net effect is, if the user chooses 'cancel' on the dialogue the private variable is never changed - the PropertyChanged event fires, rebinding the selected index to the existing value.
Hope this is what you were trying to accomplish.
UPDATE (11/10/2012) - Alternate method (possibly for SL5?). Write code behind to tie into SelectionChanged event of your TabControl, reset the tab control's selected item property based on your test.
private void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0)
{
MessageBoxResult result = MessageBox.Show("Do you want to save?", "Really?", MessageBoxButton.OKCancel);
if (result != MessageBoxResult.OK)
{
((TabControl)sender).SelectionChanged -= new SelectionChangedEventHandler(TabControl_SelectionChanged);
((TabControl)sender).SelectedItem = e.RemovedItems[0];
((TabControl)sender).SelectionChanged += new SelectionChangedEventHandler(TabControl_SelectionChanged);
}
}
}

Resources