Hi,
I created a simple application (runs out of browser) to show RadTreeView with ability to change node name. A simple click on RadTreeViewItem should select node and the second click on same node should enter node name to edit mode. It works perfect! But... after it I created another application containing a simple button only. And this button should launch the first application with RadTreeView. And ... "something happens" - the selection works as well and edit mode is entered but after it "something" steals focus and edit mode is finished exactly after beginning. How this problem could be resolved?
A code is following:
RafTreeView application
I created a simple application (runs out of browser) to show RadTreeView with ability to change node name. A simple click on RadTreeViewItem should select node and the second click on same node should enter node name to edit mode. It works perfect! But... after it I created another application containing a simple button only. And this button should launch the first application with RadTreeView. And ... "something happens" - the selection works as well and edit mode is entered but after it "something" steals focus and edit mode is finished exactly after beginning. How this problem could be resolved?
A code is following:
RafTreeView application
A code behind of View - RadTreeViewDemo.xaml.cs :public
partial
class
RadTreeViewDemo : UserControl
{
public
RadTreeViewDemo(TreeViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
}
private
Scenario selectedItem;
private
void
ListTreeView_MouseLeftButtonUp(
object
sender, MouseButtonEventArgs e)
{
if
( e.OriginalSource
is
TextBlock )
{
if
( ( e.OriginalSource
as
TextBlock ).DataContext
is
Scenario )
{
Scenario newSelectedItem = ( e.OriginalSource
as
TextBlock ).DataContext
as
Scenario;
if
(
this
.selectedItem != newSelectedItem )
{
if
(
this
.selectedItem !=
null
)
{
this
.selectedItem.IsEditable =
false
;
}
this
.selectedItem = newSelectedItem;
}
else
{
this
.selectedItem.IsEditable =
true
;
}
}
}
}
}
A View - RadTreeViewDemo.xaml :<
UserControl
x:Class
=
"RadControlsSilverlightApp1.RadTreeViewDemo"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
xmlns:telerikInput
=
"clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
xmlns:Application
=
"clr-namespace:RadControlsSilverlightApp1"
mc:Ignorable
=
"d"
d:DesignWidth
=
"640"
d:DesignHeight
=
"480"
>
<
UserControl.Resources
>
<
ResourceDictionary
>
<
Style
x:Key
=
"DefaultRadTreeViewStyle"
TargetType
=
"telerik:RadTreeView"
>
<
Setter
Property
=
"telerik:AnimationManager.AnimationSelector"
>
<
Setter.Value
>
<
telerik:AnimationSelector
>
<
telerik:TreeViewExpandCollapseAnimation
AnimationName
=
"Expand"
Direction
=
"In"
/>
<
telerik:TreeViewExpandCollapseAnimation
AnimationName
=
"Collapse"
Direction
=
"Out"
/>
</
telerik:AnimationSelector
>
</
Setter.Value
>
</
Setter
>
<
Setter
Property
=
"SelectionMode"
Value
=
"Single"
/>
<
Setter
Property
=
"ItemEditTemplate"
>
<
Setter.Value
>
<
DataTemplate
>
<
TextBox
/>
</
DataTemplate
>
</
Setter.Value
>
</
Setter
>
<
Setter
Property
=
"Template"
>
<
Setter.Value
>
<
ControlTemplate
TargetType
=
"telerik:RadTreeView"
>
<
Grid
x:Name
=
"RootElement"
>
<
Border
BorderBrush
=
"Black"
BorderThickness
=
"1"
Background
=
"Blue"
>
<
Grid
>
<
ScrollViewer
x:Name
=
"ScrollViewer"
>
<
ItemsPresenter
/>
</
ScrollViewer
>
<
Grid
x:Name
=
"DragBetweenItemsFeedback"
HorizontalAlignment
=
"Left"
Height
=
"8"
IsHitTestVisible
=
"False"
Margin
=
"{TemplateBinding Padding}"
Visibility
=
"Collapsed"
VerticalAlignment
=
"Top"
>
<
Grid.ColumnDefinitions
>
<
ColumnDefinition
Width
=
"8"
/>
<
ColumnDefinition
/>
</
Grid.ColumnDefinitions
>
<
Ellipse
HorizontalAlignment
=
"Left"
Height
=
"8"
Stroke
=
"{StaticResource DragBetweenItemsFeedback_BackgroundBrush}"
StrokeThickness
=
"2"
VerticalAlignment
=
"Center"
Width
=
"8"
/>
<
Rectangle
Grid.Column
=
"1"
Fill
=
"{StaticResource DragBetweenItemsFeedback_BackgroundBrush}"
Height
=
"2"
Margin
=
"-2,3,0,0"
RadiusY
=
"2"
RadiusX
=
"2"
VerticalAlignment
=
"Top"
/>
</
Grid
>
</
Grid
>
</
Border
>
</
Grid
>
</
ControlTemplate
>
</
Setter.Value
>
</
Setter
>
</
Style
>
<
telerik:ContainerBindingCollection
x:Key
=
"TreeViewItemContainerBinding"
>
<
telerik:ContainerBinding
PropertyName
=
"IsInEditMode"
Binding
=
"{Binding IsEditable, Mode=TwoWay}"
/>
<
telerik:ContainerBinding
PropertyName
=
"IsExpanded"
Binding
=
"{Binding IsExpanded, Mode=TwoWay}"
/>
</
telerik:ContainerBindingCollection
>
<
telerik:HierarchicalDataTemplate
x:Key
=
"ScenarioDefinitionItemTemplate"
telerik:ContainerBinding.ContainerBindings
=
"{StaticResource TreeViewItemContainerBinding}"
>
<
StackPanel
Orientation
=
"Horizontal"
>
<
TextBlock
x:Name
=
"Title"
Text
=
"{Binding Name, Mode=TwoWay}"
VerticalAlignment
=
"Center"
HorizontalAlignment
=
"Left"
Width
=
"30"
TextTrimming
=
"WordEllipsis"
Margin
=
"5,0,0,0"
>
</
TextBlock
>
</
StackPanel
>
</
telerik:HierarchicalDataTemplate
>
<!-- TreeView Common Style -->
<
Style
x:Key
=
"CommonInputFilesTreeViewStyle"
TargetType
=
"telerik:RadTreeView"
BasedOn
=
"{StaticResource DefaultRadTreeViewStyle}"
>
<
Setter
Property
=
"BorderBrush"
Value
=
"Black"
/>
<
Setter
Property
=
"Background"
Value
=
"DarkGrey"
/>
<
Setter
Property
=
"SelectionMode"
Value
=
"Extended"
/>
<
Setter
Property
=
"IsDragDropEnabled"
Value
=
"True"
/>
<
Setter
Property
=
"ItemsIndent"
Value
=
"20"
/>
</
Style
>
<
DataTemplate
x:Key
=
"ScenarioPathItemEditTemplate"
>
<
TextBox
Text
=
"{Binding Name, Mode=TwoWay}"
FontFamily
=
"Arial"
FontSize
=
"10"
/>
</
DataTemplate
>
</
ResourceDictionary
>
</
UserControl.Resources
>
<
Grid
x:Name
=
"LayoutRoot"
Background
=
"Azure"
>
<
telerik:RadTreeView
x:Name
=
"ListTreeView"
Background
=
"AliceBlue"
ItemsSource
=
"{Binding ScenarioDefinitionList, Mode=TwoWay}"
ItemTemplate
=
"{StaticResource ScenarioDefinitionItemTemplate}"
SelectedItem
=
"{Binding CurrentScenario, Mode=TwoWay}"
SelectionMode
=
"Single"
ItemEditTemplate
=
"{StaticResource ScenarioPathItemEditTemplate}"
IsEditable
=
"True"
MouseLeftButtonUp
=
"ListTreeView_MouseLeftButtonUp"
>
</
telerik:RadTreeView
>
</
Grid
>
</
UserControl
>
A View Model TreeViewModel.cs:An entity - Scenariopublic
class
TreeViewModel : INotifyPropertyChanged
{
public
TreeViewModel()
{
ScenarioDefinitionList =
new
ObservableCollection<Scenario>()
{
new
Scenario(
"first"
),
new
Scenario(
"second"
),
new
Scenario(
"third"
),
};
CurrentScenario = ScenarioDefinitionList[0];
}
private
ObservableCollection<Scenario> scenarioDefinitionList;
public
ObservableCollection<Scenario> ScenarioDefinitionList
{
get
{
return
this
.scenarioDefinitionList; }
set
{
this
.scenarioDefinitionList = value;
RaisePropertyChanged( () => ScenarioDefinitionList );
}
}
private
Scenario currentScenario {
get
;
set
; }
public
Scenario CurrentScenario
{
get
{
return
this
.currentScenario; }
set
{
this
.currentScenario = value;
RaisePropertyChanged( () => CurrentScenario );
}
}
#region INotifyPropertyChanged implementation
protected
void
RaisePropertyChanged<T>(Expression<Func<T>> lambda)
{
var name = PropertySupport.ExtractPropertyName<T>( lambda );
NotifyPropertyChanged( name );
}
public
void
NotifyPropertyChanged(
string
propertyName)
{
var handler =
this
.PropertyChanged;
if
( handler !=
null
)
handler(
this
,
new
PropertyChangedEventArgs( propertyName ) );
}
public
event
PropertyChangedEventHandler PropertyChanged;
#endregion
}
And the second application (launcher) - xaml:public
class
Scenario : INotifyPropertyChanged
{
public
Scenario(
string
name)
{
this
.Name = name;
}
private
bool
isEditable;
public
bool
IsEditable
{
get
{
return
isEditable; }
set
{ isEditable = value;
RaisePropertyChanged( () => IsEditable );
}
}
private
string
name;
public
string
Name
{
get
{
return
name; }
set
{
name = value;
RaisePropertyChanged( () => Name );
}
}
#region INotifyPropertyChanged implementation
protected
void
RaisePropertyChanged<T>(Expression<Func<T>> lambda)
{
var name = PropertySupport.ExtractPropertyName<T>( lambda );
NotifyPropertyChanged( name );
}
public
void
NotifyPropertyChanged(
string
propertyName)
{
var handler =
this
.PropertyChanged;
if
( handler !=
null
)
handler(
this
,
new
PropertyChangedEventArgs( propertyName ) );
}
public
event
PropertyChangedEventHandler PropertyChanged;
#endregion
}
and behind code - xaml.cs:<
UserControl
x:Class
=
"SilverlightApplication9.MainPage"
mc:Ignorable
=
"d"
d:DesignHeight
=
"300"
d:DesignWidth
=
"400"
>
<
Grid
x:Name
=
"LayoutRoot"
Background
=
"White"
>
<
Button
Content
=
"Run"
Height
=
"23"
HorizontalAlignment
=
"Left"
Margin
=
"80,49,0,0"
Name
=
"button1"
VerticalAlignment
=
"Top"
Width
=
"75"
Click
=
"button1_Click"
/>
</
Grid
>
</
UserControl
>
I tried it with behaviors, triggers, event handlers in code behind (as in example) and all are with same result. Could somebody advice something?public
partial
class
MainPage : UserControl
{
public
MainPage()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, RoutedEventArgs e)
{
Window window =
new
Window();
TreeViewModel viewModel =
new
TreeViewModel();
Application.Current.MainWindow.Dispatcher.BeginInvoke( () =>
{
RadTreeViewDemo scenarioBuilderMainView =
new
RadTreeViewDemo( viewModel );
window.Content = scenarioBuilderMainView;
} );
window.Width = 1097;
window.Height = 700;
window.Left = ( Application.Current.Host.Content.ActualWidth - window.Width ) / 2;
window.Top = ( Application.Current.Host.Content.ActualHeight - window.Height ) / 2;
window.Show();
}
}