Adding Wrap Panel in to a Stack Panel - c#

Why does this no work? The Stack Panel has default settings Buttons shouldn't be out of border.
void AddWrapPanel() {
WrapPanel myWrapPanel = new WrapPanel();
myWrapPanel.Background = System.Windows.Media.Brushes.Azure;
myWrapPanel.Orientation = Orientation.Horizontal;
myWrapPanel.Width = 200;
myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
myWrapPanel.VerticalAlignment = VerticalAlignment.Top;
// Define 3 button elements. The last three buttons are sized at width
// of 75, so the forth button wraps to the next line.
Button btn1 = new Button();
btn1.Content = "Button 1";
btn1.Width = 200;
Button btn2 = new Button();
btn2.Content = "Button 2";
btn2.Width = 75;
// Add the buttons to the parent WrapPanel using the Children.Add method.
myWrapPanel.Children.Add(btn1);
myWrapPanel.Children.Add(btn2);
this.stackPanel1.Children.Add(myWrapPanel);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
AddWrapPanel();
}
here the XAML
<Window x:Class="AmpelThingy.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>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="349,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<StackPanel Height="214" HorizontalAlignment="Left" Margin="32,33,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="392" />
</Grid>
</Window>
its a simle problem i dont know what more details i can write

Here you have created a method "button1_Click" but how does WPF know when it should actuallly call it? So, you have to hook buttons click event to "button1_Click" method. You can do it using Click="button1_Click" property. Now your XAML will look like :
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="349,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<StackPanel Height="214" HorizontalAlignment="Left" Margin="32,33,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="392" />
</Grid>
Hope this will help you..!

Related

WPF Window drag and drop not working

Why destination Image image1 is not changing after I drag Image img2 on it.
My XAML Code:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="463.42" Width="861.214">
<Grid>
<Image Name="image1" Margin="291,10,297,94" AllowDrop="True" Source="C:/img/0.png" Drop="img1_Drop"/>
<Button x:Name="btn1" Content="merge" HorizontalAlignment="Left" Margin="753,392,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="add" HorizontalAlignment="Left" Margin="658,392,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
Here is C# code:
private void img1_Drop(object sender, DragEventArgs e)
{
image1.Source = (System.Windows.Media.ImageSource)e.Data.GetData(typeof(System.Windows.Media.ImageSource));
//image1.Source = (BitmapImage)e.Data.GetData(DataFormats.Bitmap);
}
Nothing happens, how to solve this problem ?
You can read the image directly from dropped filename info like this:
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
var filename = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
image1.Source = new BitmapImage(new Uri(filename));
}

ListView item has 0 width while rendered when ContentControl hidden

I have a problem with ListView while the ContentControl it is in is hidden.
My program uses XMPP and updates when players connect to the Chat server. When the ContentControl is visible while someone connects it looks like this:
But if they connect and the ContentControl is invisible when it renders it looks like this (once set visible again):
However, if you hover in just the right spot you can see that the control is still there, just not rendered:
Here is my UserControl:
<UserControl x:Class="LegendaryClient.Controls.ChatPlayer"
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"
Height="50" Width="250">
<Grid>
<Rectangle Fill="#02000000" />
<!-- Allow entire control to be hovered over -->
<Image x:Name="ProfileImage" HorizontalAlignment="Left" Height="40" Margin="5,5,0,0" VerticalAlignment="Top" Width="40" Source="/LegendaryClient;component/Icon.ico" />
<Label x:Name="LevelLabel" Content="30" HorizontalAlignment="Left" Margin="5,20,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="SemiBold">
<Label.Effect>
<DropShadowEffect ShadowDepth="2" BlurRadius="1" />
</Label.Effect>
</Label>
<Label x:Name="PlayerName" Content="Snowl" HorizontalAlignment="Left" Margin="45,0,0,0" VerticalAlignment="Top" FontWeight="Bold" Foreground="White" />
<Label x:Name="PlayerStatus" Content="Test status" HorizontalAlignment="Left" Margin="45,20,0,0" VerticalAlignment="Top" Foreground="White" />
</Grid>
</UserControl>
And my render function:
if (Client.UpdatePlayers)
{
Client.UpdatePlayers = false;
ChatListView.Items.Clear();
foreach (KeyValuePair<string, ChatPlayerItem> ChatPlayerPair in Client.AllPlayers.ToArray())
{
if (ChatPlayerPair.Value.Level != 0)
{
ChatPlayer player = new ChatPlayer();
player.Width = 250;
player.Tag = ChatPlayerPair.Value;
player.PlayerName.Content = ChatPlayerPair.Value.Username;
player.LevelLabel.Content = ChatPlayerPair.Value.Level;
player.PlayerStatus.Content = ChatPlayerPair.Value.Status;
var uriSource = new Uri(Path.Combine(Client.ExecutingDirectory, "Assets", "profileicon", ChatPlayerPair.Value.ProfileIcon + ".png"), UriKind.RelativeOrAbsolute);
player.ProfileImage.Source = new BitmapImage(uriSource);
player.ContextMenu = (ContextMenu)Resources["PlayerChatMenu"];
player.MouseMove += ChatPlayerMouseOver;
player.MouseLeave += player_MouseLeave;
ChatListView.Items.Add(player);
}
}
}

Hide/Display XAML elements or block LostFocus event

I have a page containing two StackPanels, each containing one TextBox and one Button:
<StackPanel x:Name="Row1">
<TextBox x:Name="TextBox1" Text="" GotFocus="OnFocusHandler" LostFocus="OffFocusHandler"/>
<Button x:Name="Button1" Content="Convert" Click="OnClickHandler" Visibility="Collapsed"/>
</StackPanel>
<StackPanel x:Name="Row2">
<TextBox x:Name="TextBox2" Text="" GotFocus="OnFocusHandler" LostFocus="OffFocusHandler"/>
<Button x:Name="Button2" Content="Convert" Click="OnClickHandler" Visibility="Collapsed"/>
</StackPanel>
I would like to do the following:
When a textbox has focus, the other textbox must be hidden and the corresponding button must show
When a textbox is out of focus, we are back to the original display: only empty textboxes are visible
I don't want the button to be able to trigger the OffFocusHandler
This is the current code that I have for the three handlers:
private void OnFocusHandler(object sender, RoutedEventArgs e)
{
TextBox SenderTextBox = (TextBox)sender;
if (SenderPanel.Name == "TextBox1")
{
Button1.Visibility = Visibility.Visible;
}
else if (SenderPanel.Name == "TextBox2")
{
Button2.Visibility = Visibility.Visible;
}
}
private void OffFocusHandler(object sender, RoutedEventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
Button1.Visibility = Visibility.Collapsed;
Button2.Visibility = Visibility.Collapsed;
}
private void OnClickHandler(object sender, RoutedEventArgs e)
{
// some stuff unrelated to my issue
}
How do I avoid the button clicking to trigger the OffFocusHandler code?
Is there another way to code this? I'm a complete beginner so I may not think the right way.
You can just Bind to the TextBox.IsFocused property in Xaml, and use the BooleanToVisibilityConverter to show/hide the button.
Example:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="300" Width="400" Name="UI" >
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolTovisible" />
</Window.Resources>
<Grid>
<StackPanel x:Name="Row1" Height="54" VerticalAlignment="Top">
<TextBox x:Name="TextBox1" Text="" />
<Button x:Name="Button1" Content="Convert" Visibility="{Binding ElementName=TextBox1, Path=IsFocused, Converter={StaticResource BoolTovisible}}"/>
</StackPanel>
<StackPanel x:Name="Row2" Margin="0,60,0,0" Height="51" VerticalAlignment="Top">
<TextBox x:Name="TextBox2" Text="" />
<Button x:Name="Button2" Content="Convert" Visibility="{Binding ElementName=TextBox2, Path=IsFocused, Converter={StaticResource BoolTovisible}}"/>
</StackPanel>
</Grid>
</Window>
for each element, there is a Visibility tag, it is "Visible" by default but you can assign "Hidden" or "Collapsed" as follow:
<RadioButton Margin="20,118,318,-43" GroupName="MCSites" Visibility="Hidden">
Radio Button Description
</RadioButton>

Add Usercontrol To A Canvas

I have seen may examples one of them been
Add WPF control at runtime
Seem this solution has work for a lot of people. What the hell am I doing wrong? My Label won't Show on the canvas.
Label l = new Label();
l.Background = new LinearGradientBrush(Colors.Black, Colors.Black, 0);
canBackArea.Children.Add(l);
l.Visibility = System.Windows.Visibility.Visible;
l.Content = "Hello";
Canvas.SetLeft(l,20);
Canvas.SetTop(l, 20);
Canvas.SetZIndex(l, lableList.Count);
Canvas Has a white color, Thus the Background.
canBackArea is a Canvas
XML CODE
<ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Visible">
<Canvas Name="canBackArea"
Width="500"
Height="300"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Background="White"
MouseMove="canBackArea_MouseMove">
<telerik:RadContextMenu.ContextMenu>
<telerik:RadContextMenu Name="mnuBack"
ItemClick="ContextMenu_ItemClick"
Opened="mnuBack_Opened">
<telerik:RadMenuItem Name="mBackground" Header="Set Background Image" />
<telerik:RadMenuItem Name="mSize" Header="Set Size" />
<telerik:RadMenuItem Name="mLable" Header="Add Text" />
<telerik:RadMenuItem Name="mChangeText" Header="Change Text" />
</telerik:RadContextMenu>
</telerik:RadContextMenu.ContextMenu>
<Image Name="imgBackground" />
</Canvas>
</ScrollViewer>
After Add a lot of labels.
This is my MainWindow.xaml
<Window x:Class="WpfApplication1.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">
<Canvas x:Name="canBackArea">
</Canvas>
and this is my codebehind.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Label l = new Label();
canBackArea.Children.Add(l);
l.Visibility = System.Windows.Visibility.Visible;
l.Content = "Hello";
Canvas.SetLeft(l, 20);
Canvas.SetTop(l, 20);
}
This works perfectly fine.
http://i.imgur.com/JooqS.png
It could depend on the context your using it in?
I have tried to reproduce your issue and I suspect the possible issues should be that
No Foreground color is set to Label controls.
zIndex should be more than any other children controls I think as Canvas.SetZIndex(l, canBackArea.Children.Count);
Below is what I have tried and tested.
XAML Code
<Window x:Class="TestApplication.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" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="269*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Content="New Label Content" Height="30" />
<TextBox x:Name="txtLabelContent" Width="200" Height="30"></TextBox>
<Button Margin="10 0 0 0" Height="30" Width="70" Click="Button_Click">Add Label</Button>
</StackPanel>
<Canvas Grid.Row="1" x:Name="canBackArea" Background="White" Grid.RowSpan="2">
</Canvas>
</Grid>
</Window>
Window code-behind
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace TestApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Label l = new Label();
l.Background = new LinearGradientBrush(Colors.Black, Colors.Black, 0);
l.Foreground = new LinearGradientBrush(Colors.White, Colors.White, 0);
canBackArea.Children.Add(l);
l.Visibility = System.Windows.Visibility.Visible;
l.Content = txtLabelContent.Text;
Canvas.SetLeft(l, 20);
Canvas.SetTop(l, 20);
Canvas.SetZIndex(l, canBackArea.Children.Count);
}
}
}
using your xaml and this
private void mnuBack_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
Label l = new Label();
canBackArea.Children.Add(l);
l.Visibility = System.Windows.Visibility.Visible;
l.Content = "Hello";
Canvas.SetLeft(l, 20);
Canvas.SetTop(l, 20);
Canvas.SetZIndex(l, lableList.Count);
lableList.Add(l);
}
I can add labels
The Problem was i was using styles and it is some how over writing my lable, i replaced it with textbox and every thing seems fine....

Create 2 tabs dynamically with different contents wpf

The following code creates 2 tabs on the click of a button, but does not display any content. I'm still learning this and want to know what the mistake is.
Code behind
public void button2_Click(object sender, RoutedEventArgs e)
{
popUp1.IsOpen = false;
Canvas canvas4 = new Canvas();
ScrollViewer viewer4 = new ScrollViewer();
viewer4.Content = canvas4;
string txtfl = File.ReadAllText(textBox1.Text);
TextBlock txbl1 = new TextBlock();
txbl1.Text = txtfl;
canvas4.Children.Add(txbl1);
TabItem newTab1 = new TabItem();
newTab1.Header = "Text File";
newTab1.Content = viewer4;
tabctrl1.Items.Add(newTab1);
tabctrl1.SelectedItem = newTab1;
Brush br = new SolidColorBrush(Colors.White);
Canvas canvas2 = new Canvas();
canvas2.Background = br;
ScrollViewer viewer2 = new ScrollViewer();
viewer2.Content = canvas2;
TabItem newTab2 = new TabItem();
newTab2.Header = "Test";
newTab2.Content = viewer2;
tabctrl1.Items.Add(newTab2);
}
TabControl is created using XAML
XAML
<Window ... >
<Grid>
<Popup Name="popUp1" Width="500" Height="300" Placement="Center" IsEnabled="False" IsOpen="False">
<Canvas x:Name="myCanvas1" Width="500" Height="300" Visibility="Visible">
<Button Content="Button" Height="38" HorizontalAlignment="Left" Margin="12,71,0,0" Name="button1" VerticalAlignment="Top" Width="151" Click="button2_Click" />
</Canvas>
</Popup>
<TabControl Name="tabctrl1" Width="1345" Height="28" Margin="0,24,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="2">
<TabControl.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF3A60AD" Offset="0.528"/>
<GradientStop Color="#FF6A85D8" Offset="0.01"/>
</LinearGradientBrush>
</TabControl.Background>
</TabControl>
</Grid>
</Window>
Hi the reason your code does not appear to work is because the Height of the tab control is set to 28.
Try changing it to 280, you should then be able to see the content.
<TabControl Name="tabctrl1" Width="1345" Height="280" Margin="0,24,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="2">

Categories