Tabitem Header's button name from code behind - wpf

I have a TabControl, and I am adding TabItems dynamically to it . I have a predefined DataTemplate in the TabControl's resources. So, there is a TextBox and a Button in that template. I bind that template with the TabItem dynamically and add that tab item to tab control.
Here is the code :
<Window x:Class="Menu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="tab" Height="465" Width="869">
<Window.Resources>
</Window.Resources>
<Grid>
<TabControl Name="tab" Height="200" HorizontalAlignment="Stretch" verticalAlignment="Top" >
<TabControl.Resources >
<DataTemplate x:Key="TabHead" DataType="TabItem" >
<WrapPanel >
<Label Name="lbl1" Content="tab1" ></Label>
<Button Name="btn1" Content="X"></Button>
</WrapPanel>
</DataTemplate>
</TabControl.Resources>
</TabControl>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="55,246,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
this is XAML code and here is VB.NET code :
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim tabi As New TabItem
tabi.HeaderTemplate = TryCast(tab.Resources("TabHead"), DataTemplate)
tab.Items.Add(tabi)
End Sub
Now I want to have a unique name for each button which is in the DataTemplate of the TabControl's resources.
How can I set unique names for each Button in my DataTemplate?

Related

Touch event in wpf interfering with other events

Help!
Been wrestling with a problem using Touch events in a wpf application I am developing.
Let me set the scene...
I have a main window (fmMainWindow) that has a Textbox on it. The window is coded to be centred to the screen when open. On the TouchUp event of the Textbox, I have code that opens up another window (fmKeypad_Numeric) using the syntax ShowDialog. This window is also centred. It is a numeric keypad with numbered buttons on it.
My problem is that the Touch Event used on the main window is having an affect on the Click events of the buttons on the window it opens.
It looks like wherever on the screen the Touch Event occurred, that the button on the next window opened at the same position seems to initially have some sort of 'focus' (it appears as if the mouse is hovered over it).
If you click the button that initially has the 'focus', the click event fires as expected.
If however you first click one of the other buttons that doesn't have the 'focus' its click event does not fire. It takes a second click for it to fire??
I have used Snoop to see how the events fire and when you click one of the other buttons that doesn't initially have the 'focus', Events related to the button that has the 'focus' fire??
Anyone got any ideas? I've spent days on this one.
Code for the two windows is below
Many thanks in advance,
Rhyd.
fmMainWindow
<Window x:Class="fmMainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" SizeToContent="WidthAndHeight" ResizeMode="NoResize" FontFamily="Century Gothic">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
<TextBox Name="tbTest" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="180,10,10,0" Width="150" Height="50" BorderBrush="#FF1B378B" Background="#FFEC1212"/>
</Grid>
Public Class fmMainWindow
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen
End Sub
Private Sub tbTest_TouchUp(sender As Object, e As TouchEventArgs) Handles tbTest.TouchUp
e.Handled = True
Dim myKeypad As New fmKeypad_Numeric()
myKeypad.ShowDialog()
myKeypad.Close()
End Sub
End Class
fmKeypad_Numeric
<Window x:Class="fmKeypad_Numeric"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Keypad_Numeric" SizeToContent="WidthAndHeight" ResizeMode="NoResize" FontFamily="Century Gothic" FontSize="30" Left="10">
<Window.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Height" Value="70" />
</Style>
</Window.Resources>
<Grid Name ="grKeypad" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBox Name="tbNumber" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalContentAlignment="Right" Text="" Width="230" IsReadOnly="True" Margin="10,10,0,0"/>
<Button Name="bu7" Content="7" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,70,0,0" Width="70" />
<Button Name="bu8" Content="8" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="90,70,0,0" Width="70" />
<Button Name="bu9" Content="9" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,70,10,0" Width="70" />
<Button Name="bu4" Content="4" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,150,0,0" Width="70" />
<Button Name="bu5" Content="5" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="90,150,0,0" Width="70" />
<Button Name="bu6" Content="6" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,150,10,0" Width="70" />
<Button Name="bu1" Content="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,230,0,0" Width="70" />
<Button Name="bu2" Content="2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="90,230,0,0" Width="70" />
<Button Name="bu3" Content="3" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,230,10,0" Width="70" />
<Button Name="bu0" Content="0" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,310,0,0" Width="70" />
</Grid>
Public Class fmKeypad_Numeric
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen
End Sub
Private Sub Button_Click(sender As Object, e As RoutedEventArgs) Handles bu0.Click, bu1.Click, bu2.Click, bu3.Click, bu4.Click, bu5.Click, bu6.Click, bu7.Click, bu8.Click, bu9.Click
Dim myButtonName As String
Dim myTextToAdd As String = ""
myButtonName = CType(sender, Button).Name
myTextToAdd = Replace(myButtonName, "bu", "")
Me.tbNumber.Text = Me.tbNumber.Text & myTextToAdd
End Sub
End Class

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?

Set binding on button tooltip with a declared instance of a user control as source

I've added a user control (with labels and textboxes) to my WPF project shown here:
<UserControl x:Class="MyProject.myControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="384" Width="543">
<Grid x:Name="_LabelServerURL">
<Label x:Name="_LabelServerUrl" Content="Server Url:" HorizontalAlignment="Left" Margin="60,21,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<TextBox x:Name="_TextboxUrl" HorizontalAlignment="Left" Height="23" Margin="158,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="235"/>
<Label x:Name="_LabelURLExample" Content="(https://promodel.com)" HorizontalAlignment="Left" Margin="398,21,0,0" VerticalAlignment="Top" Width="139"/>
</Grid>
</UserControl>
and have created an instance of that user control in my main window code behind.
public partial class MainWindow : Window
{
private myControl _settings = new myControl();
public MainWindow()
{
InitializeComponent();
AddStackPanelChildren();
_ButtonEPSSettings.ToolTip = _epsSettings;
//SetButtonTootips();
}
In my main window I have a list of buttons, and I want to set their tooltip property (or bind the tooltip property) to the instance of my user control so that the tooltip dynamically updates when I add values to the textboxes in my user control. So far in my main window xaml I've been able to show my user control as the tooltip shown here:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls ="clr-namespace:MyProject" <!--Declare project namespace-->
Title="EPS Configuration Manager" Height="415" Width="766">
<Grid>
<Button x:Name="_ButtonSettings"
Content="EPS Settings"
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="199"
HorizontalContentAlignment="Left"
BorderThickness="0"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="_ButtonSettings_Click"
>
<Button.ToolTip>
<ToolTip>
<StackPanel HorizontalAlignment="Left" Height="384" Margin="0,0,0,0" VerticalAlignment="Top" Width="543" Name="_StackPanelEPSSettings">
<Controls:myControl/>
</StackPanel>
</ToolTip>
</Button.ToolTip>
</Button>
I am still not clear on setting that tooltip as the instance of my user control as declared in my main window code behind. Any help will go a long way. Thanks.
If you set tooltips in backgroud, don't use these code:
<Button.ToolTip>
<ToolTip>
<StackPanel HorizontalAlignment="Left" Height="384" Margin="0,0,0,0" VerticalAlignment="Top" Width="543" Name="_StackPanelEPSSettings">
<Controls:myControl/>
</StackPanel>
</ToolTip>
</Button.ToolTip>
If you want the tooltip dynamically updates:
private myControl mytooltip = new myControl();
public MainWindow()
{
InitializeComponent();
AddStackPanelChildren();
mytooltip._TextboxUrl.Text = "Hello";
_ButtonEPSSettings.ToolTip = mytooltip;
//SetButtonTootips();
}

How to active UserControl outside the wrapper?

I met a Focus related problem in UserControl:
Suppose we have a UserControl like this:
<UserControl x:Class="_20130826.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<ListBox>
<ListBoxItem>
<TextBlock Text="text1" />
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="text2" />
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="text3" />
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="text4" />
</ListBoxItem>
</ListBox>
</StackPanel>
</UserControl>
And the MainWindow.xaml like this:
<Window x:Class="_20130826.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:temp="clr-namespace:_20130826"
Title="MainWindow">
<StackPanel>
<Button Content="Deactive UserControl" />
<StackPanel>
<Button Name="Button1" Content="Active UserControl" />
<ContentControl>
<temp:UserControl1 />
</ContentControl>
</StackPanel>
</StackPanel>
</Window>
Step 1: Click text1 TextBlock inside UserControl, and default the
background changes to deeper.
Step 2: Click 'Deactive UserControl' button, so the text1 background
turns to lighter.
Step 3: Click 'Active UserControl' button, then ...
I want the text1 background changed to deeper, which means the UserControl has been focused/actived.
How can I achieve this?
If you want to set the Focus to the UserControl when the Button("Active UserControl") gets pressed, we can add a Click event handler and assign focus to the UserControl.
So say something like:
<Window x:Class="_20130826.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:temp="clr-namespace:_20130826"
Title="MainWindow">
<StackPanel>
<Button Content="Deactive UserControl" />
<StackPanel>
<Button Name="Button1" Content="Active UserControl" Click="Button1_OnClick" />
<ContentControl>
<temp:UserControl1 x:Name="myUserControl" />
</ContentControl>
</StackPanel>
</StackPanel>
</Window>
and in MainWindow.xaml.cs:
private void Button1_OnClick(object sender, RoutedEventArgs e) {
FocusManager.SetFocusedElement(this, myUserControl);
}
Now when you click the "Active UserControl" Button, Focus will switch to the UserControl. However this will not give you that "deeper" state on the ListBoxItem as it still doesnt have Focus. So to sort that
In UserControl1.xaml.cs add:
protected override void OnGotFocus(RoutedEventArgs e) {
base.OnGotFocus(e);
if (!Equals(e.OriginalSource, this))
return;
TraversalRequest tRequest = new TraversalRequest(FocusNavigationDirection.Next);
MoveFocus(tRequest);
}
What this does is when the UserControl gets focus, it moves Focus to the next element within it which would be the element you clicked initially.
You can get a demo of your code tweaked with this: Here

Object Reference not set error while trying to set the visiblity of grid

I was trying to set the visiblity of a Grid from code behind.
grdStopTimeOut.Visibility = Windows.Visibility.Visible
I have declared this grid in XAML and have set the visiblity to Visible.
<Grid Name="grdTimeTStopCondition" Visibility="Hidden" Margin="0,29,0,-6">
Somehow when the application runs the grid is coming as nothing and the exceptions is thrown.
Anybody have any idea why it is happening??
XAML File
<Page x:Class="Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Name="cmbStopConditions" VerticalAlignment="Top" Width="86" SelectedIndex="0">
<ComboBoxItem Content="Expression"></ComboBoxItem>
<ComboBoxItem Content="Manual"></ComboBoxItem>
</ComboBox>
<Grid Name="grdStopTimeOut" Visibility="Visible" >
<Label Content="Timeout" Height="28" HorizontalAlignment="Left" Name="lblTimeout_stopcond" VerticalAlignment="Top" Margin="0,29,0,0" />
<TextBox Height="23" HorizontalAlignment="Left" Name="txtStopTimeout" VerticalAlignment="Top" Width="30" Margin="60,29,0,0" />
<Label Content="secs" Height="28" HorizontalAlignment="Left" Name="lblTimeoutSec_stopCond" VerticalAlignment="Top" Width="39" Margin="105,24,0,0" />
</Grid>
</Grid>
</Page>
CodeBehind
Class Page1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub cmbStopConditions_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cmbStopConditions.SelectionChanged
Dim item As ComboBoxItem = TryCast(cmbStopConditions.SelectedItem, ComboBoxItem)
If item IsNot Nothing Then
If Convert.ToString(item.Content) = "Expression" Then
grdStopTimeOut.Visibility = Windows.Visibility.Visible
ElseIf Convert.ToString(item.Content) = "Manual" Then
grdStopTimeOut.Visibility = Windows.Visibility.Hidden
End If
End If
End Sub
End Class
regards,
SKB
Edit: The handler can occur before the grid is initialized, so a null-check is in order.
Are you calling this before InitializeComponent? That for one would explain the lack of a reference because all the fields are hooked up with the named controls in that method.

Resources