RadTabControl reloading content in swiching tabitems - wpf

I'm using RadTabControl and I have a problem with reloading tabs. If I add two tabs and edit first tab content and go to the second tab and come back first tab lost the content. If I edit some tab content and click to edited tab title and go to another tab and come back it binds and changes context. I have a lost focus event for textbox(content) if I set break point to lost focus event and after running I test all situation it's good working but I need to change Note.Content property in lost focus by binding Content textbox with content property. Content property is in Note class.
xmlns:ec="clr-namespace:WpfControls;assembly=WpfControls"
<UserControl.Resources>
<DataTemplate x:Key="TabContent">
<Grid>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBox Text="{Binding Content}" Name="ContentTextBox" MinLines="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextWrapping="Wrap" AcceptsReturn="True"/>
</ScrollViewer>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid Grid.Column="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ec:NoteBook x:Name="uiNotebook" TabContentTemplate="{StaticResource TabContent}" Margin="30" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
public DataTemplate TabContentTemplate
{
get { return (DataTemplate)GetValue(TabContentTemplateProperty); }
set { SetValue(TabContentTemplateProperty, value); }
}
// Using a DependencyProperty as the backing store for ContentTemplate. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TabContentTemplateProperty =
DependencyProperty.Register("TabTemplate", typeof(DataTemplate), typeof(NoteBook), new PropertyMetadata(null));
Can anybody help me? Thanks Jamshed

After long researches I find the answer and I want to share it. it was IsContentPreserved property and I set it to true, it works!
<telerik:RadTabControl x:Name="tabControl" IsContentPreserved="True" ItemsSource="{Binding Tabs}">

Related

display content class xaml vb.net

Well i have a mainWindow written in xaml (vb.net) with 10 buttons and for each button i have a class (with buttons...) in my project. Example: when i click on button valid i display the content of the class valid in my grid. Each class is instantiated in the class mainWdow.
I also have a grid on this mainWindow.
When i click on a button in my mainWindow, i would like to display the content of the correspondig class in my grid and of course i want to be able to use this class (click on buttons...).
What's the best way to do this on vb.net / xaml please?
On this webpage http://msdn.microsoft.com/en-us/library/cc903947(v=vs.95).aspx there are some indications but it is not what i want because i don't want just only display the content on my class but i want to be able to work with the class displayed on my grid...
I give you thanks for your advices.
Ok, so first you should read up about DataTemplates from the Data Templating Overview page on MSDN. Now here is the basic principal... instead of putting your reusable XAML into Windows, declare them in DataTemplates in your Window.Resources section:
<DataTemplate x:Key="StopDataTemplate">
<!--Define your content here-->
</DataTemplate>
<DataTemplate x:Key="ValidDataTemplate">
<!--Define your content here-->
</DataTemplate>
Now you can display the content of these DataTemplates in a ContentControl:
<ContentControl Name="Content" ContentTemplate="{StaticResource ValidDataTemplate}" />
Then when you want to switch to the other view, you just need to set the ContentControl.ContentTemplate property to the other DataTemplate (from Window code behind):
DataTemplate dataTemplate = (DataTemplate)FindResource("StopDataTemplate");
Content.ContentTemplate = dataTemplate;
Well, here is what i have done to do some tests:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="CBTemplate">
<Grid Height="200" HorizontalAlignment="Left" Name="centralGrid" VerticalAlignment="Top" Width="200" Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" Text="{Binding Title}"
Margin="10" HorizontalAlignment="Left" FontSize="20"/>
</Grid>
</DataTemplate>
then in my mainWindow.xaml i have this:
<Grid x:Name="layout" Background="LightBlue" Margin="250,150,260,380">
<ContentControl Name="Content" ContentTemplate="{StaticResource CBTemplate}" />
</Grid>
And in the mainWindow.vb i have:
Class MainWindow
Private t As Thing
Sub New()
InitializeComponent()
t = New Thing("test")
layout.DataContext = t
End Sub
End Class
So my program compiles properly but there is a little problem: text="{Binding Title}" doesn't work and when i replace it with "toto", there is no problem toto is displayed...
Did i miss something in my code?

Need a toggle menu that closes when it loses focus

This is going to be difficult to explain but bear with me.
I'm creating an application in WPF using the MVVM pattern. I'm new to it which is why I'm asking this question.
I have the application set up as 3 pages or views within a window. One of these is static and is always there, the other two are a couple of small settings pages that open in the top corner over the top of everything using zindex. At the moment the menu that opens these pages uses a listbox with togglebuttons as it's template (the checked state is bound to the listbox) so that you can click to open the menu, then click the button again to close it.
In an ideal world I'd like it so that if the menu page were to lose focus (listen for a click on the static view?) the settings views close too. Also I wondered if anyone had a simpler solution for a menu that works in a similar way because at the moment it is a pretty messy solution. Here are some code samples:
<ListBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding PageViewModels}" SelectedItem="{Binding CurrentPageViewModel}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Margin="10,0" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FCCC"/>
<ToggleButton
VerticalAlignment="Stretch"
Content=""
IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- Settings views -->
<ContentControl Panel.ZIndex="2" Grid.Row="1" Grid.Column="0" Content="{Binding CurrentPageViewModel}"/>
<!-- Main page view -->
<ContentControl Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Width="1000" Height="700" Content="{Binding StaticPageViewModel}"/>
I'm using the concepts in this blog post to manage my views and viewmodels, however I changed the way the menu is shown so I could remove the need for a change page command/ICommand.
TL;DR : I'm looking for suggestions and criticism with what I could do to improve the way I've currently created my menu bar.
I would create an attached property if you really wanna do the MVVM style to close the View when it loses focus.
public static readonly DependencyProperty CloseViewOnLostFocusProperty =
DependencyProperty.RegisterAttached("CloseViewOnLostFocus", typeof (object), typeof (MainWindow), new FrameworkPropertyMetadata(default(object), RegisterLostFocus));
private static void RegisterLostFocus(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
//This is the control that is attached to it e.g. ContentControl
var sender = dependencyObject as FrameworkElement;
//Your ViewModel
var viewModel = dependencyPropertyChangedEventArgs.NewValue as ViewModel;
if (sender != null)
{
sender.LostFocus += (o, args) =>
{
//Close whatever you are doing right now to close the View.
viewModel.Close();
};
}
}
And on your view you can attach whatever ViewModel you want to close when it loses focus e.g. your SettingsView got LostFocus it'll close that view. In here I created an attached property on my MainWindow class.
<!-- Settings views -->
<ContentControl
MainWindow.CloseViewOnLostFocus="{Binding RelativeSource={RelativeSource Self},Path=Content}"
x:Name="SettingsView" Panel.ZIndex="2" Grid.Row="1" Grid.Column="0" Content="{Binding CurrentPageViewModel}"/>
<!-- Main page view -->
<ContentControl x:Name="MainPageView" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Width="1000" Height="700" Content="{Binding StaticPageViewModel}"/>
You can have a ContextMenu attached to the button, which is opened when the button is clicked. This way, the close-when-unfocused behaviour is entirely automatic.
You can then just restyle the menu to look however you want.

How to use up and down key to move treeviewitem selection in WPF

We create a HierarchicalDataTemplate for a treeview control. We can use mouse to click the tree item to change the selection. Now, we want to use keyboard up and down key to move the selection up and down. But it seems that it can't work. I searched a lot by Google and Stackoverflow, but failed.
So I created a new thread for this, could you please give me some help? thx.
<HierarchicalDataTemplate x:Uid="HierarchicalDataTemplate_1" x:Key="My_data_template" >
<ContentControl x:Uid="ContentControl_1" MouseDoubleClick="MouseDoubleClick" MouseRightButtonDown="MouseRightClick">
<Grid x:Uid="Grid_2" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Uid="ColumnDefinition_1" Width="*"/>
<ColumnDefinition x:Uid="ColumnDefinition_2" Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel x:Uid="StackPanel_3" HorizontalAlignment="Left" Orientation="Horizontal" Grid.Column="0">
<TextBlock x:Uid="TextBlock_13" Text="{Binding Name}" VerticalAlignment="Center" Margin="3,0,0,1" TextWrapping="NoWrap"/>
</StackPanel>
<CheckBox x:Uid="CheckBox_3" HorizontalAlignment="Right" Click="CheckBox_Click" Grid.Column="1" ToolTip="On/Off">
</CheckBox>
</Grid>
</ContentControl>
</HierarchicalDataTemplate>
Another question is that, I can use mouse to click the textblock to select the item, but when I use mouse click the CheckBox, the item can't be selected. Is there anyway to make treeview item selected when I click the CheckBox?
The way I applied the template to treeview is as following:
<TreeView x:Name="tv_pointcloud" x:Uid="TreeListView_1"
ItemTemplateSelector="{StaticResource DataAccessor}"
......
/>
public class DataAccessor : DataTemplateSelector
{
public DataAccessor()
{
Init();
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var element = container as FrameworkElement;
var template = element.FindResource("My_data_template") as DataTemplate;
return template;
}
......
}
thanks.
I had the same problem as you, in a WPF treeview I was unable to use Arrow Keys to navigate. The problem I found was the Checkbox that was getting the focus. So I set "Focusable = False for the checkbox, and now the navigation in the treeview is as expected:
<CheckBox Focusable="False" ... />
Keyboard commands and such are called gestures. Perhaps this is a good place to get you started:
Keyboard shortcuts in WPF

Problem in define Orientation Property For My UerControl in WPF?

i have textbox and progressbar in my usercontrol that place in stackpanel.
i write a Orientation Property but it doesnt work???
if orientation set to horizontal,textbox and progressbar should be place horizontally and if orientation set to vertically,textbox and progressbar should be place Vertically.
my code is :
<StackPanel Orientation="Horizontal">
<TextBox Name="TValue" Width="40" Height="23" TextChanged="TValue_TextChanged"/>
<ProgressBar Name="PG1" Width="200" Height="23"/>
</StackPanel>
and my property is:
//Define Orientation Property
public Orientation Orientation
{
get { return (Orientation)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
// Using a DependencyProperty as the backing store for Orientation. This enables animation, styling, binding, etc...
public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register("Orientation", typeof(Orientation), typeof(TPUserControl), new UIPropertyMetadata(Orientation.Horizontal));
if u can help me,u save me from storm,THNX
If I understand your problem correctly, you have:
Created a UserControl (lets call it MyUserControl).
Created a dependency property Orientation for MyUserControl.
Within usercontrol you have a stackpanel and you want to bind the Orientation of this stackpanel with Orientation property of MyUserControl.
Try this:
<StackPanel Orientation="{Binding Orientation,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type MyUserControl }}}">
<TextBox Name="TValue" Width="40" Height="23" TextChanged="TValue_TextChanged"/>
<ProgressBar Name="PG1" Width="200" Height="23"/>
</StackPanel>
Suggestions:
Be more explicit while asking for help. You stated a problem (and that
too not clearly) and then didn't even mention the question. :)
You may want to check if defining a ControlTemplate helps in your scenario.

WPF TextBlock Binding to DependencyProperty

I have what I believe to be about one of the most simple cases of attempting to bind a view to a dependencyproperty in the view model. It seems that the initial changes are reflected in the view but other changes to the DP do not update the view's TextBlock. I'm probably just missing something simple but I just can't see what it is. Please take a look...
My XAML has a status bar on the bottom of the window. I want to bind to the DP "VRAStatus".
<StatusBar x:Name="sbar" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"
VerticalAlignment="Bottom" Background="LightBlue" Opacity="0.4" DockPanel.Dock="Bottom" >
<StatusBarItem>
<TextBlock x:Name="statusBar" Text="{Binding VRAStatus}" />
</StatusBarItem>
<StatusBarItem>
<Separator Style="{StaticResource StatusBarSeparatorStyle}"/>
</StatusBarItem>
</StatusBar>
My viewmodel has the DP defined:
public string VRAStatus
{
get { return (string)GetValue(VRAStatusProperty); }
set { SetValue(VRAStatusProperty, value); }
}
// Using a DependencyProperty as the backing store for VRAStatus.
public static readonly DependencyProperty VRAStatusProperty =
DependencyProperty.Register("VRAStatus", typeof(string), typeof(PenskeRouteAssistViewModel),new PropertyMetadata(string.Empty));
Then, in my code I set the DP:
VRAStatus = "Test Message...";
Is there something obvious here that I am missing? In my constructor for the viewmodel I set the DP like this:
VRAStatus = "Ready";
I never get the Test Message to display.
You need to add DataContext="{Binding RelativeSource={RelativeSource Self}} in .
As it turns out things were a little more complicated than I had thought (like, when is that NOT the case :) My RibbonControl was in a UserControl to get all of that XAML out of the MainWindow. It was the fact that it was in a UserControl that made it work differently with the ViewModel. I don't know why - probably one of those mysteries that won't ever be solved. But by putting my RibbonControl directly on the MainWindow, everything works as expected - both with a DP and a C# Property. Interesting. (Wish I could get back those two days of my life!)
thanks,
Bill
Try specifying the name of the DP with the Path flag in the binding like this:
<TextBlock x:Name="statusBar" Text="{Binding Path=VRAStatus}">
Bill,
When and where do you set the DataContext? I had problems in the past that when I set the DataContext before the InitializeComponent, my Bindings never executed properly.
Also, for curiosity's sake: why do you use a DP in your ViewModel instead of just a Property?
Try to specify the UpdateSourceTrigger property of the Binding:
<StatusBar x:Name="sbar" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"
VerticalAlignment="Bottom" Background="LightBlue" Opacity="0.4" DockPanel.Dock="Bottom" >
<StatusBarItem>
<TextBlock x:Name="statusBar" Text="{Binding VRAStatus, UpdateSourceTrigger=PropertyChanged}" />
</StatusBarItem>
<StatusBarItem>
<Separator Style="{StaticResource StatusBarSeparatorStyle}"/>
</StatusBarItem>
</StatusBar>

Resources