WPF app - not clickable anymore - c#

I am new to WPF and suddendly my Window is not clickable anymore.
I cannot click one button and I cannot edit a text box.
I am using Visual Studio 2015 (new to it as well).
What could be the reason?
here some code:
<Window x:Class="Mxx.MainWindow"
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"
xmlns:local="clr-namespace:Mxx"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="26*"/>
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="Log out" Click="MenuItem_Click1"/>
<MenuItem Header="Option 2">
<MenuItem Header="Load new control" Click="MenuItem_Click2a"/>
<MenuItem Header="Open new window" Click="MenuItem_Click2b"/>
</MenuItem>
</Menu>
<Border x:Name="Stage" Grid.Row="1"/>
<Grid x:Name="LoginLayer" Background="#FFFFFFFF" Grid.RowSpan="2">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Background="LightBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Mxx" Grid.ColumnSpan="2" FontWeight="Bold" HorizontalAlignment="Center" Margin="5" FontSize="20"/>
<TextBlock Text="Name" Grid.Row="1" Margin="3"/>
<TextBox x:Name="txtName" Grid.Row="1" Grid.Column="1" Margin="3" MinWidth="100" HorizontalAlignment="Left"/>
<TextBox x:Name="txtName1" Background="Azure" Grid.Row="4" Grid.Column="1" Margin="3" MinWidth="100" HorizontalAlignment="Left"/>
<TextBlock Text="Password" Grid.Row="2" Margin="3"/>
<PasswordBox x:Name="txtPassword" Grid.Column="1" Grid.Row="2" Margin="3" MinWidth="100" HorizontalAlignment="Left"/>
<Button Click="Login_Click" Content="Log in" Grid.Row="3" Grid.Column="1" Margin="3"/>
</Grid>
<TextBlock x:Name="lat1" Text="lat" Grid.Row="4" Margin="3" />
</Grid>
</Grid>
and here my main window
public MainWindow()
{
InitializeComponent();
_geoLocator = new Geolocator();
Utils.getNetworkAdapterId();
Debug.WriteLine("imei: " + Properties.Settings.Default.imei);
Properties.Settings.Default.imei = "imei1";
Properties.Settings.Default.Save(); // Saves settings in application setting
}
my debugger hits the last line

Your problem is with the last declared TextBlock
<TextBlock x:Name="lat1" Text="lat" Grid.Row="4" Margin="3" />
That is outside of the grid that has row and column definitions, and so it is defaulting to taking up all the space in the page as an overlay. You are unable to give focus to the controls beneath it.
You can see this in the XAML editor window. In the code view, click on that element, and in the designer you will see that the textblock is the full size of your window. Because it is declared last in the XAML, it take the uppermost position (z-index) and therefore "hides" the controls behind it.

You have a TextBlock covering the other controls. I think you want to move lat1 into the /Grid tag directly above it.

Related

C# WPF - How to Change a 'Preferences' Window's Displayed Grid using a ListView?

I am currently in the process of making a 'Preferences' window in my C# WPF program.
The aim of this window is to have a ListView on the left, and a list of tweakable controls on the right.
Each item in the ListView directly corresponds to a Grid that contains all of the controls under the selected item's content.
Once the item in the ListView is changed, the current grid's visibility must be collapsed, and the grid that corresponds to the item selected's visibility must be changed to visible.
I thought that DataBinding might work for this, however I have no idea how to use it. Could someone please inform me how to implement this feature?
I have only one grid at the moment. The whole window looks like this:
<Window x:Class="DarkOrbit_Skill_Price_Calculator.DarkOrbit_Skill_Price_Calculator___Preferences"
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"
xmlns:local="clr-namespace:DarkOrbit_Skill_Price_Calculator"
mc:Ignorable="d"
Title="DarkOrbit Skill Price Calculator - Preferences" Height="360" Width="640" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ListView Name="ListView_PreferenceOption" Width="150" Margin="5" SelectionChanged="SelectionChanged_ListView_PreferenceOption">
<ListViewItem IsSelected="True">
<StackPanel Orientation="Horizontal">
<Image Source="Images\Installing Updates.png" Height="35"/>
<TextBlock Text="Update" FontSize="14" FontFamily="Segoe UI" VerticalAlignment="Center" Margin="5"/>
</StackPanel>
</ListViewItem>
</ListView>
<Grid Margin="5" Name="Grid_Update" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Content="Update Version Architecture" VerticalAlignment="Center"/>
<ComboBox IsReadOnly="True" Width="100" Margin="5">
<ComboBoxItem Content="64-Bit"/>
<ComboBoxItem Content="32-Bit" IsSelected="True"/>
</ComboBox>
</StackPanel>
</Grid>
</Grid>
</Window>
I have absolutely no clue as to how to swap the grid depending on the index selected.
You can bind the visibility of each grid to the selected state of its corresponding list item by referencing the ListViewItem element. Something like this:
<Grid>
<Grid.Resources>
<BooleanToVisibilityConverter x:Key="Converter" />
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ListView Width="150" Margin="5">
<ListViewItem IsSelected="True" x:Name="One">
<TextBlock Text="Update" FontSize="14" FontFamily="Segoe UI"
VerticalAlignment="Center" Margin="5"/>
</ListViewItem>
<ListViewItem IsSelected="False" x:Name="Two">
<TextBlock Text="Foo" FontSize="14" FontFamily="Segoe UI"
VerticalAlignment="Center" Margin="5"/>
</ListViewItem>
</ListView>
<Grid Margin="5" Grid.Column="1"
Visibility="{Binding IsSelected, ElementName=One, Converter={StaticResource Converter}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Content="Update Version Architecture" VerticalAlignment="Center"/>
<ComboBox IsReadOnly="True" Width="100" Margin="5">
<ComboBoxItem Content="64-Bit"/>
<ComboBoxItem Content="32-Bit" IsSelected="True"/>
</ComboBox>
</StackPanel>
</Grid>
<Grid Margin="5" Grid.Column="1"
Visibility="{Binding IsSelected, ElementName=Two, Converter={StaticResource Converter}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Content="Update Something Else" VerticalAlignment="Center"/>
<ComboBox IsReadOnly="True" Width="100" Margin="5">
<ComboBoxItem Content="64-Bit"/>
<ComboBoxItem Content="32-Bit" IsSelected="True"/>
</ComboBox>
</StackPanel>
</Grid>
</Grid>
For those who are having the same predicament and want to use C# code to make this work, I eventually thought of the following code:
StackPanel[] allGrids = { Grid_1, Grid_2, Grid_3, Grid_4, ... }; //Replace StackPanel with the
//type of control you are using, e.g. Grid or WrapPanel.
foreach (StackPanel grid in allGrids)
{
grid.Visibility = Visibility.Collapsed; //collapse all grids
}
allGrids[(your listview/other control).SelectedIndex].Visibility = Visibility.Visible;
//make the grid you need visible

WPF XAML Using Mouse Action Event

I am learning the WPF 4.5 Unleashed book. When I tried to incorporate Mouse Action Event such as MouseEnter or MouseDoubleClick for the Button by typing the code manually, the compiler told me it could not find the reference for the Mouse Action Event. However, when I use the double Tab shortcut, everything works well. What could possibly be the issue? I have bold the trouble code below.
<Window x:Class="WpfApplicationLearning0001.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="513.265" Width="748.469">
<DockPanel>
<Menu DockPanel.Dock="Top">
</Menu>
<StackPanel Name="ButtonBar" Orientation="Horizontal" DockPanel.Dock="Right">
<StackPanel.LayoutTransform>
<RotateTransform Angle="90">
</RotateTransform>
</StackPanel.LayoutTransform>
**<Button Name="Panel1Button" MouseEnter="Panel1Button_MouseEnter">
Toolbox
</Button>**
</StackPanel>
<Grid Background="White" Margin="0,0,2,3">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Background="Black" Foreground="White" HorizontalContentAlignment="Center" Content= "Start Page"/>
<GroupBox Grid.Row="1" Grid.Column="0" Background="White" Foreground="Black" Header="Start" />
<GroupBox Grid.Row="2" Grid.Column="0" Background="White" Foreground="Black" Header="Recent" />
<GroupBox Grid.Row="3" Grid.Column="0" Background="White" Foreground="Black" Header="Option" />
<GroupBox Grid.Row="1" Grid.Column="1" Grid.RowSpan="3" Background="White" Foreground="Black" Header="Get Start">
<ListBox>
<ListBoxItem>Article number1</ListBoxItem>
<ListBoxItem>Article number2</ListBoxItem>
<ListBoxItem>Article number3</ListBoxItem>
<ListBoxItem>Article number4</ListBoxItem>
</ListBox>
</GroupBox>
</Grid>
</DockPanel>
The double click not only puts it in the XAML code, put it also creates the code behind in the .cs file of:
private void Panel1Button_MouseEnter(object sender, MouseEventArgs e)
{
}
Until you add the code behind yourself, if you are doing it manually, then the error is correct. In addition, when manually coding be sure to include (object sender, MouseEventArgs e) in the function call or it may not recognize it as a valid function call to the MouseEvent.

How do I set a wpf text box to resize automatically when the user changes the size of dialog box?

How do I set a wpf text box to resize automatically when the user changes the size of dialog box?
<Window x:Class="MemoPad.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="LightGray"
Title="Window1" Height="350" Width="700" >
<StackPanel Orientation="Vertical">
<Menu DockPanel.Dock ="Right">
<MenuItem Header="Find" x:Name="gMNuFind" />
</Menu>
<Button Content=" Find "
Margin="5,10,5,5"
x:Name="gBuFind"
/>
<TextBox Margin="0,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
MinHeight="270" MinWidth="690"
x:Name = "gTBxInfo"
TextWrapping="Wrap"
AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</StackPanel>
Or change StackPanel to Grid
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="LightGray"
Title="Window1" Height="350" Width="700" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="Find" x:Name="gMNuFind" />
</Menu>
<Button Grid.Row="1" Content=" Find "
Margin="5,10,5,5"
x:Name="gBuFind"
/>
<TextBox Grid.Row="2" Margin="0,0,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
MinHeight="270" MinWidth="690"
x:Name = "gTBxInfo"
TextWrapping="Wrap"
AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</Grid>
</Window>
Remove MinHeight and MinWidth from the TextBox, and change HorizonalAlignment to Stretch
<TextBox Margin="0,0,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
x:Name = "gTBxInfo"
TextWrapping="Wrap"
AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto" />
Edit:
If you want to resize the TextBox in both directions (horizontally and vertically), you would have to use a different container other than StackPanel, so that the TextBox sizing is independent.
Something like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="Find" x:Name="gMNuFind" Grid.Row="0"/>
</Menu>
<Button x:Name="gBuFind"
Content=" Find "
Margin="5,10,5,5"
Grid.Row="1"/>
<TextBox x:Name = "gTBxInfo"
Margin="0,0,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
TextWrapping="Wrap"
AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Grid.Row="2"/>
</Grid>

Childwindow WPF extended toolkit not opening

This is my code to open childwindow:
ImageLocation location = new ImageLocation();
location.WindowStartupLocation = Xceed.Wpf.Toolkit.WindowStartupLocation.Center;
location.Show();
But the childwindow doesn't show at all.
This is my xaml for childwindow:
<xctk:ChildWindow x:Class="CXLocalSearch.Dialogs.ImageLocation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Caption="Image Path"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Height="64" Width="400">
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="63.95"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Margin="2" TextWrapping="Wrap" Text="Image Path" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<StackPanel Grid.Column="1" HorizontalAlignment="Left" Margin="3,2,0,2" Orientation="Horizontal" >
<TextBox x:Name="txtPath" Margin="0,2" TextWrapping="Wrap" VerticalAlignment="Center" Width="250"/>
<Button x:Name="btnSave" Content="Save" Click="btnSave_Click" Width="60" Margin="3,0,0,0"/>
</StackPanel>
</Grid>
</xctk:ChildWindow>
Could anybody please clarify what the issue is?
From the looks of it, you've separated your ChildWindow into a separate control. That's fine, but it needs to be hosted inside a primary window in order to ever become visible. Start with the simplest thing possible:
<Window>
<Grid>
<Button Click="...">Click to Show</Button>
<xctk:ChildWindow x:Name="childWindow">
<TextBlock>Hello!</TextBlock>
</xctk:ChildWindow>
</Grid>
</Window>
I think you'll find this works fine (assumes event hookup), so take it from there.

WPF Listbox with textbox

I have a listbox with a textbox
The textbox is defined in a datatemplate that ends like this
<TextBlock Grid.Row="4" Grid.Column="0" Text="Note"></TextBlock>
<TextBox Height="Auto" Grid.Row="4" Grid.Column="1"
Text="{Binding PartData.Note}" AcceptsReturn="True" TextWrapping="Wrap" >
</TextBox>
</Grid>
I want the textbox to expand when the user enters multiple rows but it doesnt. The rowdefintion height is set to *
I've tried your sample with this code, and it works (use Shift-Enter to start a new row inside TextBox)
<Window x:Class="TextBoxWrap.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Height="140" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Note"/>
<TextBox Margin ="10, 0,0,0" Height="Auto" Grid.Row="1" Grid.Column="1" Text="{Binding Count}"
AcceptsReturn="True" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
You need to add
VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"
to TextBox so it takes all available space.

Categories

Resources