WPF load background image on form - wpf

Hey all I have the following code that supposed to take my jpg image and place it on the background of the form. However, all I see is a black background.
Dim myBrush As New ImageBrush()
Dim image As New Image()
Dim grid As New Grid()
image.Source = New BitmapImage(New Uri("pack://application:,,,/WpfApplication1;component/Resources/1680-logoless.jpg"))
myBrush.ImageSource = image.Source
grid.Background = myBrush
And this is my current XAML:
<Window x:Class="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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Margin="157,145,0,0"/>
<Label x:Name="label1" Content="Label" HorizontalAlignment="Left" Margin="68,59,0,0" VerticalAlignment="Top" Width="111"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="335,62,0,0"/>
<Button x:Name="button1" Content="Encrypt" HorizontalAlignment="Left" Height="17" Margin="335,123,0,0" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="toEncrypt" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="172" Margin="335,145,0,0"/>
<TextBox x:Name="toDecrypt" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="172" Margin="335,173,0,0"/>
<Button x:Name="button2" Content="Decrypt" HorizontalAlignment="Left" Height="23" Margin="335,201,0,0" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
Anyone see anything I could be missing?

Set the x:Name attribute of the Grid in XAML, which will create a field in your MainWindow class:
<Window ...>
<Grid x:Name="rootGrid">
...
</Grid>
</Window>
Then, instead of creating a new Grid instance, set the Background of the one declared in XAML:
rootGrid.Background = New ImageBrush(New BitmapImage(
New Uri("pack://application:,,,/Resources/1680-logoless.jpg")))

Related

Collapsed Tab Item Prevents Control Select in WPF Designer

I'm using Visual Studio Pro 2019, and I came across an issue when using the tab control. I created a test project to test switching between collapsed tabs programmatically, but found that, when in the designer, I'm unable to select the control within the tab when the tabs are collapsed. I found this answer confirming that the tab being collapsed is the issue.
My question is whether there is a fix for this that isn't just turning the tabs to visible, as it shifts the item's grid location.
The alternate solution could be to manually alternate the visibility of overlapping grids, but tabs seem to be the more appropriate solution.
xaml:
<Window x:Class="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:delete3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Background="#FF424242">
<Grid>
<TabControl Name="Tabs" HorizontalAlignment="Left" Height="255" Margin="122,51,0,0" VerticalAlignment="Top" Width="437">
<TabItem Name="First" Header="TabItem" Visibility="Collapsed">
<Grid Background="#FFE5E5E5">
<TextBox HorizontalAlignment="Left" Height="23" Margin="143,129,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="29"/>
</Grid>
</TabItem>
<TabItem Name="Second" Header="TabItem" Visibility="Collapsed">
<Grid Background="#FFE5E5E5">
<TextBox HorizontalAlignment="Left" Height="23" Margin="195,90,0,0" TextWrapping="Wrap" Text="2" VerticalAlignment="Top" Width="29"/>
</Grid>
</TabItem>
</TabControl>
<Button Content="1" HorizontalAlignment="Left" Margin="191,359,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="2" HorizontalAlignment="Left" Margin="484,379,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>

How to move focus to previous element from textbox in wpf?

I have a sample UI and trying to move focus to previous element on pressing Key Up. Can someone help me how to move focus to previous textbox from current Textbox or another elements.
Here is my Sample UI
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:Sample_Application"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel PreviewKeyDown="DockPanel_PreviewKeyDown" PreviewKeyUp="DockPanel_PreviewKeyUp" KeyUp="DockPanel_KeyUp" >
<TextBox DockPanel.Dock="Top" Margin="18,0,0,0" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBox DockPanel.Dock="Top" Margin="18,8,0,0" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<CheckBox DockPanel.Dock="Top" Margin="18,8,0,0" Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox DockPanel.Dock="Top" Margin="18,8,0,0" Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<RadioButton DockPanel.Dock="Top" Margin="18,8,0,0" Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<RadioButton DockPanel.Dock="Top" Margin="18,8,0,0" Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<StackPanel>
<TextBox HorizontalAlignment="Left" Margin="153,34,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Margin="153,10,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="153,10,0,0" VerticalAlignment="Top"/>
</StackPanel>
</DockPanel>
</Window>
Below is my code behind
private void DockPanel_PreviewKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up && e.OriginalSource is TextBox)
{
FocusNavigationDirection focusDirection = FocusNavigationDirection.Previous;
TraversalRequest request = new TraversalRequest(focusDirection);
UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
// Change keyboard focus.
if (elementWithFocus != null)
{
if (elementWithFocus.MoveFocus(request)) e.Handled = true;
}
e.Handled = true;
}
}

Why the datagrid is not shown?

I have a main windows with a button that open another window that has a user control and this user control has a datagrid. The code is this:
Main windows: code of the button:
dlgImprimirControlUsuarioView miView = new dlgImprimirControlUsuarioView();
miView.Show();
The code in my second window (codebehind):
public partial class dlgImprimirControlUsuarioView : Window
{
public dlgImprimirControlUsuarioView()
{
InitializeComponent();
UserControlView miView = new UserControlView();
this.ccControlUsuario = miView;
}
}
AXML of the second windows:
<Window x:Class="WpfObtenerTamañoControles.dlgImprimirControlUsuarioView"
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:WpfObtenerTamañoControles"
mc:Ignorable="d"
Title="dlgImprimirControlUsuario" Height="200" Width="300">
<Grid>
<ScrollViewer Name="svControlUsuario" HorizontalAlignment="Left" Height="200" Width="300" Margin="0,0,0,0" VerticalAlignment="Top" >
<ContentControl Name="ccControlUsuario" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top"/>
</ScrollViewer>
</Grid>
</Window>
The AXML of the user control that is used by the second windows:
<UserControl x:Class="WpfObtenerTamañoControles.UserControlView"
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"
xmlns:local="clr-namespace:WpfObtenerTamañoControles"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" Margin="0,0,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<ScrollViewer Name="svDatagrid" HorizontalAlignment="Left" Height="32" Margin="0,0,0,0" VerticalAlignment="Top" Width="300">
<DataGrid Name="dgDatagrid" HorizontalAlignment="Left" Height="120" Margin="0,0,0,0" VerticalAlignment="Top" Width="Auto">
<DataGrid.Columns>
<DataGridTextColumn Header="Contador" Binding="{Binding}" Width="2.5cm"/>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</Grid>
</UserControl>
I can see the vertical scroll bar of the scroll viewer of the second window, but I can't see the content of this scroll viewer. But I am not able to see the problem.
Thanks.

Creating ContextBinding XAML

I'm having problems setting the DataContext for all fields that are inside a stackPanel. What I would like to do is set the Data Context as vm:ViewModel. But it's not working and when I ask VS for assistance with DataBinding it Displays the TextBox.DataContext inside the TextBox. Is there a way to only set it once or do I have to set it for each control?
<StackPanel DataContext="vm:ViewModel">
<TextBox Text="{Binding FirstNumber}" HorizontalAlignment="Left" Height="23" Margin="206,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120">
<TextBox.DataContext>
<vm:ViewModel/>
</TextBox.DataContext>
</TextBox>
</StackPanel>
You need to get your DataContext to refer to an instance of your ViewModel.
DataContext="vm:ViewModel" is not creating an instance of the ViewModel...it's just setting a string.
Use Property Element syntax instead:
<StackPanel>
<StackPanel.DataContext>
<vm:ViewModel/>
</StackPanel.DataContext>
<TextBox Text="{Binding FirstNumber}" HorizontalAlignment="Left" Height="23" Margin="206,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</StackPanel>
And if you do that, then there's no need/in fact it's probably wrong to create another one in your TextBox.DataContext...you just want to inherit the DataContext of the StackPanel.
I'll just show you another way to create a ViewModel and to refer to the instance of it so you have a bigger picture.....that is to create it in resources, and then refer to that resource in the binding...here's an example:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfApplication8"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<vm:ViewModel x:Key="myviewmodel"/>
</Window.Resources>
<StackPanel x:Name="stackp" DataContext="vm:ViewModel">
<TextBox Text="{Binding Source={StaticResource myviewmodel}, Path=FirstNumber}" HorizontalAlignment="Left" Height="23" Margin="206,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</StackPanel>
</Window>
Another example that sets the DataContext at a higher level in the tree:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfApplication8"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:ViewModel/>
</Window.DataContext>
<StackPanel>
<TextBox Text="{Binding FirstNumber}" HorizontalAlignment="Left" Height="23" Margin="206,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</StackPanel>
</Window>

Scale usercontrol in wpf?

i have multiple items in my usercontrol. I put everything in a grid. But now i am trying, if the resolution of my screen changes, the window scales automatically. This just doesn't work.
I already used viewbox, but not with the wanted result.
This is my usercontrol:
<UserControl x:Class="NewWPFVragenBeheer.Maak_toets.Views.ChangeCourse"
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"
xmlns:converters="clr-namespace:NewWPFVragenBeheer.Converters"
mc:Ignorable="d" d:DesignHeight="200" d:DesignWidth="700"
>
<UserControl.Resources>
<XmlDataProvider x:Key="Vakken"
Source="C:\Users\Ruben\Desktop\Stage 26-04\stage_TFW\stage_TFW\NewWPFVragenBeheer\Data\Courses.xml"
XPath="/Courses/Course"
/>
<converters:RadioBoolToIntConverter x:Key="radioBoolToIntConverter" />
</UserControl.Resources>
<Viewbox Stretch="None">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="722*" />
<ColumnDefinition Width="254*" />
</Grid.ColumnDefinitions>
<Label Content="Maximale tijd:" Height="28" FontWeight="Bold" HorizontalAlignment="Left" Margin="12,28,0,0" Name="label1" VerticalAlignment="Top" Width="177" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="215,30,0,0" Text="{Binding Path=MaxTime}" VerticalAlignment="Top" Width="145" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="215,2,0,0" Text="{Binding Path=ExamName,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Name="textBox1" VerticalAlignment="Top" Width="145" />
<Label FontWeight="Bold" Content="Punten:" Height="28" HorizontalAlignment="Left" Margin="386,0,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="23" Text="{Binding Path=Score,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" HorizontalAlignment="Left" Margin="567,2,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<Label Content="{Binding Path= FeedbackText, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Height="28" HorizontalAlignment="Right" Margin="0,153,527,0" Name="label3" VerticalAlignment="Top" Width="200" Foreground="#FFF50D0D" />
</Grid>
</Viewbox>
</UserControl>
this usercontrol is set in a window:
<Window x:Class="NewWPFVragenBeheer.MaakToetsDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:NewWPFVragenBeheer.Views"
Title="MaakToetsDialog"
WindowStyle ="SingleBorderWindow"
WindowState ="Maximized"
WindowStartupLocation="CenterScreen"
>
<view:MaakToetsView />
</Window>
¨please someone help.
Set the Grid to a fixed Width and Height, and set ViewBox.Stretch to Uniform. That should do it.
The correct answer likely is more complicated than the solution you seek, but I'll try to keep it short.
To do what you want, in my experience your best bet is to use a Grid as your primary initial element and then situate your controls within that Grid (or other Grids inside of it) and wrap the individual controls in ViewBoxes. After that tie the UserControl SizeChanged event to a method that forces the height and width of the UserControl to maintain an appropriate ratio.
That's the best way I have found to handle it on my WPF UIs.

Resources