tags:

views:

85

answers:

1

Hi ,

I am switching content template of a ListViewItem at run mode to enable editting the item.For that i am showing a panel with Ok and Cancel options and i need the user to select any of those option before moving to anotheritem. Means i want that panel to behave like a modal dialog. Any suggestions ?.

Advanced Thanks, Das

A: 

Hi Das :)!

You could try listen to PreviewLostKeyboardFocus event and mark it as handled when you don't want to let focus go. Here is an example. We have two columns, and if you put focus into the first column you never go out from it until you click Release Focus button:

XAML

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Focus Sample" Height="300" Width="340">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <GroupBox Header="Press Release Focus to leave">
      <StackPanel PreviewLostKeyboardFocus="StackPanel_PreviewLostKeyboardFocus">
        <TextBox/>
        <Button Content="Release Focus"
                Click="ReleaseFocusClicked"/>
      </StackPanel>
    </GroupBox>
    <GroupBox Header="Try to switch focus here:"
              Grid.Column="1">
      <TextBox/>
    </GroupBox>
  </Grid>
</Window>

C#

using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
  public partial class Window1 : Window
  {
    private bool _letGo;

    public Window1()
    {
      InitializeComponent();
    }

    private void StackPanel_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
      var uie = (UIElement) sender;
      var newFocusDO = (DependencyObject)e.NewFocus;
      if (!_letGo && !uie.IsAncestorOf(newFocusDO))
      {
        e.Handled = true;
      }
    }

    private void ReleaseFocusClicked(object sender, RoutedEventArgs e)
    {
      _letGo = true;
    }
  }
}

I'm doing one extra check to ensure whether new focus target belongs to our panel. If we don't do this we never let focus leave from the currently focused element. It worth to mention that this approach doesn't keep user from clicking on other buttons in UI. It just holds focus.

Hope this helps.

Cheers, Anvaka.

Anvaka
Hi Anvaka,Thanks for the answer but as you mentioned it will only keep keyborad focus and user can click on other buttons in the UI. I really want to restrict user from clicking on other buttons in the UI.Means want to create a "Modal" control.Thanks,Das
Das
Hi Das, another bit... If nothing else helps add handler for PreviewMouseDownEvent to the Application.Current.MainWindow and mark it handled whenever you don't want mouse buttons processing go further...
Anvaka