Create 2 tabs dynamically with different contents wpf - c#

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">

Related

Elements loaded via XamlReader() and from MainWindow.Xaml behave differently?

I have the following Viewbox in MainWindow.Xaml:
<Viewbox x:Name="R1C1Viewbox" Grid.Row="1" Grid.Column="2"
Grid.ColumnSpan="1" Grid.RowSpan="1">
<Border x:Name="R1C1Border" BorderBrush="White" CornerRadius="3"
BorderThickness="2" Background="White"
Height="40" Width="40" Margin="0,3,3,0">
<Grid x:Name="R1C1Grid">
<TextBlock x:Name="R1C1TextBlock1" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Arial" FontWeight="Bold" Text="+" FontSize="22"></TextBlock>
<Polygon x:Name="R1C1LED"
Points="0,0 15,0 0,15"
Stroke="#FFED1C24"
StrokeThickness="1">
<Polygon.Fill>
<RadialGradientBrush>
<GradientStop Color="White" Offset="2.5"/>
<GradientStop Color="Black"/>
</RadialGradientBrush>
</Polygon.Fill>
</Polygon>
<Button x:Name="R1C1Button" Background="Transparent"
BorderBrush="Transparent">
</Button>
</Grid>
</Border>
</Viewbox>
In my code behind, I define 2 RadialGradientBrushes for the Polygon.Fill property:
public static RadialGradientBrush ledOn = new RadialGradientBrush();
public static RadialGradientBrush ledOff = new RadialGradientBrush();
ledOn.GradientStops.Add(new GradientStop(Colors.Red, .8));
ledOn.GradientStops.Add(new GradientStop(Colors.White, 0));
ledOff.GradientStops.Add(new GradientStop(Colors.White, 2.5));
ledOff.GradientStops.Add(new GradientStop(Colors.Black, 0));
If I load the viewbox only from MainWindow.xaml, I can use:
R1C1LED.Fill = ledOn;
or
R1C1LED.Fill = ledOff;
and the fill changes as expected. If I load the exact same xaml from a file using XamlReader(), the Viewbox displays exactly as expected but using the code behind to change the fill as above doesn't change the fill and no errors are generated.
Example code:
private void OnTest(object sender, RoutedEventArgs e)
{
var xaml =
#"<Window
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'
mc:Ignorable='d'
Title='WPF App' Height='450' Width='800'>
<StackPanel Orientation='Vertical'>
<Button x:Name='R1C1LED' Content='Load Data' />
</StackPanel>
</Window>";
var win = (Window)XamlReader.Parse(xaml);
var button = (Button)win.FindName("R1C1LED");
button.Background = Brushes.Red;
button.Click += (obj, args) => { MessageBox.Show("Hi!"); };
win.Show();
}

WebBrowser dont show in tabitem

I try to develop WebBrowser on c#, wpf and CefSharp for experience.
I use TabControl for tabs in WebBrowser.
So i add webbrowser to tabItem.Content but it dont showing up.
public partial class MainWindow : Window
{
ChromiumWebBrowser webBrowser;
public MainWindow()
{
//Browser init
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
InitializeComponent();
textboxURL.Text = "https://www.google.com";
webBrowser = new ChromiumWebBrowser();
webBrowser.Load(textboxURL.Text);
tabItem1.Content = webBrowser;
}
//App Shutdown logic
private void Exit(object sender, StartupEventArgs e)
{
Cef.Shutdown();
}
}
This is XAML code. As you see i use DockPanel for topbar with URL and buttons.
And TabControl for tabs with browser. For now i use only one webBrowser for one tab.
Window x:Class="WebBrowser.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:WebBrowser"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="WebBrowser" Height="362" Width="699" WindowState="Maximized" WindowStartupLocation="CenterScreen">
<StackPanel HorizontalAlignment="Stretch" Margin="0,0,0,-1" Height="auto" VerticalAlignment="Stretch" >
<DockPanel VerticalAlignment="Top">
<Button x:Name="btnHome" Content="Home" Margin="2" DockPanel.Dock="Left"/>
<Button x:Name="btnBack" Content="Back" Margin="2" DockPanel.Dock="Left"/>
<Button x:Name="btnNext" Content="Next" Margin="2" DockPanel.Dock="Left"/>
<Label x:Name="labelHttp" Content="HTTP" DockPanel.Dock="Left"/>
<Button x:Name="btnMenu" Content="Menu" Margin="2" DockPanel.Dock="Right"/>
<Button x:Name="btnGo" Content="Go" Margin="2" DockPanel.Dock="Right"/>
<Button x:Name="btnRefresh" Content="Refresh" Margin="2" DockPanel.Dock="Right"/>
<TextBox x:Name="textboxURL" Margin="2"/>
</DockPanel>
<TabControl x:Name="tabControl1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,5,0,0" Height="299">
<TabItem Header="TabPage" Name="tabItem1">
</TabItem>
</TabControl>
</StackPanel>
Ok, i try to use Address property then init browser and its load my url!
So answer is dont use webBrowser.Load(textboxURL.Text); on new element but use webBrowser.Address = textBoxURL.Text;
Thanks everyone!
--With CefSharp you can with this code--
public ChromiumWebBrowser drv;
string url = "https://www.example.com";
public void syhmhfzaddtab()
{
TabPage tb = new TabPage();
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
drv = new ChromiumWebBrowser(url);
drv.Parent = tb;
tabControl1.Controls.Add(tb);
drv = new ChromiumWebBrowser(url);
drv.Dock = DockStyle.Fill;
}
Note:%100 a working code

C# WPF vertical listBox not showing vertical scrollbar

I have a listBox with some elements in it.
<StackPanel Orientation="Vertical" Grid.Row="0" >
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock x:Name="lbGroups" Text="PartPrograms Groups" FontSize="{StaticResource TEXTBOX_TITLE_FONTSIZE}" FontWeight="Bold" Margin="20" HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Center" Grid.Row="1"/>
<Button x:Name="btAddGroup" Content="" FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click"/>
<Button Name="btDeleteGroup" Content="" FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click"/>
<Button x:Name="btGroupDown" Content="" FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click"/>
<Button Name="btGroupUp" Content="" FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click"/>
</StackPanel>
<ListBox Name="lbPPgroups" Background="{x:Null}" Margin="0" ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox> <------- this is the listbox
</StackPanel>
The elements are programmatically added to the listBox with this:
void AddNewPartProgramGroup(String strContent, String strNotes, String strPathImage, bool IsChecked=false)
{
StackPanel sp = new StackPanel();
string currentDir = AppDomain.CurrentDomain.BaseDirectory.ToString();
ToggleButton toggleButton = new ToggleButton()
{
Content = strContent,
Height = IMAGES_ROW_HEIGHT / GOLDEN_RATIO,
Width = IMAGES_ROW_HEIGHT,
FontSize = 10,
Background = null,
Tag = "bt" + strContent,
ToolTip = strNotes,
Margin = new Thickness(BUTTON_MARGIN),
IsChecked = IsChecked
};
toggleButton.Click += new RoutedEventHandler(ToggleButton_Click);
sp.Children.Add(toggleButton);
Image newResizedImage = ImageUtilities.StrPath2ResizedImageSizeHeight(strPathImage, IMAGES_ROW_HEIGHT);
sp.Children.Add(newResizedImage);
sp.Orientation = Orientation.Horizontal;
sp.HorizontalAlignment = HorizontalAlignment.Left;
this.lbPPgroups.Items.Add(sp);<------ here I add elements
var newGroup = new PcDmisData.Group();
newGroup.Description = strContent;
var newImage = new PcDmisData.MyImage();
newImage.Image = newResizedImage;
newImage.IsImageEmbedded = false;
newGroup.myImage = newImage;
newGroup.Notes = strNotes;
EasyRunData.lstPPgroups.Add(newGroup);
}
the problem is after adding some elements I can't see the vertical scrollbar on the listbox:
I also tried to add a vertical scroll viewer but that didn't work.
Thanx for any help
PAtrick
So the problem is that the outer StackPanel has no real MaxHeight and the Height updates automatically. The ScrollBar only appears if this Panel reaches a certain limit in its heigth. To solve this you could play around with MaxHeight...
I would recommend to use a DockPanel.
<Grid>
<DockPanel Grid.Row="0" >
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock x:Name="lbGroups" Text="PartPrograms Groups" FontWeight="Bold" Margin="20" HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Center"/>
<Button Name="btGroupUp" Click="btGroupUp_Click" Margin="2,2,2,2" Width="30"/>
</StackPanel>
<ListBox Name="lbPPgroups" Margin="0" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
</DockPanel>
</Grid>
Just for the example in code behind:
private void btGroupUp_Click(object sender, RoutedEventArgs e)
{
for (var i=1;i<50;i++)
{
TextBox box = new TextBox();
box.Text = "Hello World " + i ;
lbPPgroups.Items.Add(box);
}
}
In this example i set ScrollViewer.VerticalScrollBarVisibility="Auto" so the ScrollBar only appears when it is needed. But you can also set it to "Visible".

Adding Wrap Panel in to a Stack Panel

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..!

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);
}
}
}

Categories