I am currently working in Windows 10 and I am working on AppBar and Command Bar controls. I created an Appbar inside which I added a commandBar which contains and app bar button. Now the issue is it is showing more icon for both commandBar and AppBar. Can you please suggest how can I remove this?
My Code is below
CommandBar commandBar = new CommandBar();
if (this.BottomAppBar == null)
{
this.BottomAppBar = new AppBar();
this.BottomAppBar.IsOpen = true;
this.BottomAppBar.IsSticky = true;
}
commandBar.PrimaryCommands.Clear();
commandBar.IsSticky = true;
commandBar.IsOpen = true;
commandBar.ClosedDisplayMode = AppBarClosedDisplayMode.Compact;
AppBarButton appBarButton = new AppBarButton();
appBarButton.Foreground = MCSExtensions.GetColorFromHex(foreground_color);
appBarButton.Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Images/submit.png", UriKind.Absolute) };
appBarButton.Label = formButton.B_NAME;
commandBar.PrimaryCommands.Add(appBarButton);
this.BottomAppBar.Content=commandBar;
The above is generating following output
What I actually want is left side bar that is commandBar, not the gray portion. Can someone suggest what I am doing wrong? I also tried just adding CommmandBar inside grid but it didn't display at all.
Update
CommandBar commandBar=new CommmandBar();
commandBar.IsOpen=true;
commandBar.IsSticky=true;
commandBar.ClosedDisplayMode=AppBarClosedDisplatMode.Compact;
AppBarButton appBarButton = new AppBarButton();
appBarButton.Foreground = MCSExtensions.GetColorFromHex(foreground_color);
appBarButton.Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Images/submit.png", UriKind.Absolute) };
appBarButton.Label = formButton.B_NAME;
commandBar.PrimaryCommands.Add(appBarButton);
pageLayoutRootGrid.Children.Add(commandBar);
I also tried adding a StackPanel in AppBar as below but not giving proper results.
if (this.BottomAppBar == null)
{
this.BottomAppBar = new AppBar();
this.BottomAppBar.IsOpen = true;
this.BottomAppBar.IsSticky = true;
}
StackPanel appBarPanel=new StackPanel();
appBarPanel.Orientation=Orientation.Horizontal;
AppBarButton appBarButton = new AppBarButton();
appBarButton.Foreground = MCSExtensions.GetColorFromHex(foreground_color);
appBarButton.Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Images/submit.png", UriKind.Absolute) };
appBarButton.Label = formButton.B_NAME;
appBarPanel.Children.Add(appBarButton);
this.BottomAppBar.Content=appBarPanel;
For xaml:
<Page.BottomAppBar>
<CommandBar>
<CommandBar.PrimaryCommands>
<AppBarButton Icon="Accept"/>
</CommandBar.PrimaryCommands>
</CommandBar>
</Page.BottomAppBar>
this line make your UI messed:
this.BottomAppBar = new AppBar();
as you can see from my xaml, create only 1 CommandBar inside BottomAppBar
Edit: if you want to use code, use this:
var commandBar = new CommandBar();
commandBar.PrimaryCommands.Add(new AppBarButton() { Label = "test" });
commandBar.VerticalAlignment = VerticalAlignment.Bottom;
this.root.Children.Add(commandBar);
UPDATE
Full xaml code:
<Page
x:Class="CommandBarSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CommandBarSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="root">
</Grid>
</Page>
The code behind:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var commandBar = new CommandBar();
commandBar.VerticalAlignment = VerticalAlignment.Bottom;
var appBarButton = new AppBarButton() { Icon = new SymbolIcon(Symbol.Accept), Label = "Accept" };
commandBar.PrimaryCommands.Add(appBarButton);
root.Children.Add(commandBar);
}
}
Related
I need to add a grid to a popup in Silverlight project. I have added this code in the mainpage.xaml.cs file I have created a popup.
but I need to add a grid showing a table from the database.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
popup p = new Popup();Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness(5.0);
StackPanel panel1 = new StackPanel();
panel1.Background = new SolidColorBrush(Colors.LightGray);
Button button1 = new Button();
button1.Content = "Close";
button1.Margin = new Thickness(2.0);
button1.Click += new RoutedEventHandler(button1_Click);
TextBlock textblock1 = new TextBlock();
textblock1.Text = "The popup control";
textblock1.Margin = new Thickness(5.0);
panel1.Children.Add(textblock1);
panel1.Children.Add(button1);
border.Child = panel1;
Grid grid1 = new Grid();
// i need to bind this grid to data from the sql database and attach this grid to the popup
// Set the Child property of Popup to the border
// which contains a stackpanel, textblock and button.
p.Child = border;
p.DataContext =
// Set where the popup will show up on the screen.
p.VerticalOffset = 200;
p.HorizontalOffset = 300;
// Open the popup.
p.IsOpen = true;
}
The Grid is a layout control, and not for displaying tables. For tabular data, you'll need a DataGrid control.
List<Customer> customerList = new List<Customer>();
var dataGrid = new DataGrid();
dataGrid.ItemsSource = customerList;
Popup popup = new Popup();
popup.Child = dataGrid;
popup.IsOpen = true;
In my C# Windows Phone 8 app, I have an AppBar. I have two icons on this AppBar, one new icon, and one edit icon. I want to change the edit icon, to the back icon whenever it is pressed, and then back to the edit icon whenever it is pressed again. I have tried this code, but I get a nullReferenceException:
public static Uri addIcon = new Uri("/Assets/AppBar/new.png", UriKind.Relative);
public static Uri backIcon = new Uri("/Assets/AppBar/back.png", UriKind.Relative);
//Edit the projects
if (editMode.Equals(false))
{
//EditMode is off, enable edit mode and modify the editprojectMenuButton
editMode = true;
editprojectMenuButton.IconUri = backIcon;
editprojectMenuButton.Text = "finish";
} else
{
//EditMode is on, disable edit mode and modify the editprojectMenubutton
editMode = false;
editprojectMenuButton.IconUri = addIcon;
editprojectMenuButton.Text = "edit";
}
Xaml code:
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton x:Name="editprojectMenuButton" IconUri="/Assets/AppBar/edit.png" Text="Edit" Click="editprojectMenuButton_Click"/>
<shell:ApplicationBarIconButton x:Name="addprojectMenuButton" IconUri="/Assets/AppBar/new.png" Text="Add" Click="addprojectMenuButton_Click"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem x:Name="aboutButton" Text="About" Click="aboutButton_Click"/>
<shell:ApplicationBarMenuItem x:Name="howtoButton" Text="How To" Click="howtoButton_Click"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
Alternate AppBar Code:
//Create the AppBar
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.IsMenuEnabled = true;
addprojectMenuBtn = new ApplicationBarIconButton(new Uri("BarIcons/add.png", UriKind.Relative));
addprojectMenuBtn.Text = "add";
addprojectMenuBtn.Click += addprojectMenuButton_Click;
editprojectMenuBtn = new ApplicationBarIconButton(new Uri("BarIcons/edit.png", UriKind.Relative));
editprojectMenuBtn.Text = "add";
editprojectMenuBtn.Click += editprojectMenuButton_Click;
ApplicationBar.Buttons.Add(addprojectMenuBtn);
ApplicationBar.Buttons.Add(editprojectMenuBtn);
It seems that when you create AppBar in xaml, your reference to Button in code behind is null.
I'm making my AppBar programatically and I've not spotted such problem:
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.IsMenuEnabled = false;
ApplicationBarIconButton addBtn = new ApplicationBarIconButton(new Uri("BarIcons/add.png", UriKind.Relative));
addBtn.Text = "Add";
addBtn.Click += addBtn_Click;
ApplicationBarIconButton infoBtn = new ApplicationBarIconButton(new Uri("BarIcons/info.png", UriKind.Relative));
infoBtn.Text = "Info";
infoBtn.Click += infoBtn_Click;
ApplicationBar.Buttons.Add(addBtn);
ApplicationBar.Buttons.Add(infoBtn);
You can also add menu items there and what you want. Then if you ApplicationBarIconButton is 'global' variable, you can change it any time, and it works.
EDIT - some explanations
Here I've found similar problem, and at this blog is explanation.
There is also a code which works:
Microsoft.Phone.Shell.ApplicationBarIconButton btn = ApplicationBar.Buttons[0] as Microsoft.Phone.Shell.ApplicationBarIconButton;
btn.IconUri = backIcon;
btn.Text = "My button";
I have this main wpf window
and this WPF page
I need to add this page to tabcontrol in main window
This is my OnRender method
protected override void OnRender(DrawingContext drawingContext)
{
if (ISFirstRender)
{
TabItem tabitem = new TabItem();
tabitem.Header = "Tab 3";
pan1.Items.Add(tabitem);
Page1 page1 = new Page1();
tabitem.Content = new Page1();
ISFirstRender = false;
}
base.OnRender(drawingContext);
}
after the application running I faced this exception while selecting the new tab
I need to know how to add wpf page to existing tabcontroll
If you want to add a new Page, as opposed to a UserControl, you can create a new Frame object and place the page in there.
if (ISFirstRender)
{
TabItem tabitem = new TabItem();
tabitem.Header = "Tab 3";
Frame tabFrame = new Frame();
Page1 page1 = new Page1();
tabFrame.Content = page1;
tabitem.Content = tabFrame;
pan1.Items.Add(tabitem);
ISFirstRender = false;
}
You can add user controls to the TabControl. So go to the add new items and select the user control and make what you want (like what you have in the page). Then add an instance of that user control to the TabControl.
protected override void OnRender(DrawingContext drawingContext)
{
if (ISFirstRender)
{
TabItem tabitem = new TabItem();
tabitem.Header = "Tab 3";
pan1.Items.Add(tabitem);
MyUserControl userControl = new MyUserControl();
tabitem.Content = userControl;
ISFirstRender = false;
}
base.OnRender(drawingContext);
}
I have a DataGrid. The ItemsSource of this DataGrid is set in the Completed event of a WCF call.However, when adding a Detail Datagrid inside the master grids DataTemplate and naming it appropriately... Ineed to fill it master grids selection change event but my codebehind does not recognise the detail grid. I cant set the ItemsSource of grdDetail like I do with grdMaster.So how i can fill my detail datagrid ?
Xaml File
<Grid x:Name="LayoutRoot">
<sdk:DataGrid x:Name="dgCustList" AutoGenerateColumns="False" Background="Transparent" SelectionChanged="dgCustList_SelectionChanged">
<sdk:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel x:Name="stkPanel">
<sdk:DataGrid x:Name="dgCustDetail" RowDetailsVisibilityMode="VisibleWhenSelected" AutoGenerateColumns="False" Background="Transparent"/>
</StackPanel>
</DataTemplate>
</sdk:DataGrid.RowDetailsTemplate>
</sdk:DataGrid>
<Grid.Projection>
<PlaneProjection x:Name="Projection"/>
</Grid.Projection>
</Grid>
And CodeBehind
public MusteriListe()
{
InitializeComponent();
var stb1 = new Storyboard { Duration = new Duration(TimeSpan.FromSeconds(1)), SpeedRatio = 3 };
var daY1 = new DoubleAnimation { From = 0.00, To = 90.00 };
Storyboard.SetTargetName(daY1, "Projection");
Storyboard.SetTargetProperty(daY1, new PropertyPath("RotationX"));
stb1.Children.Add(daY1);
this.Resources.Add("EndOfPage", stb1);
var stb = new Storyboard();
stb.Duration = new Duration(TimeSpan.FromSeconds(1));
stb.SpeedRatio = 3;
var daY = new DoubleAnimation { From = -90.00, To = 0.00 };
Storyboard.SetTargetName(daY, "Projection");
Storyboard.SetTargetProperty(daY, new PropertyPath("RotationX"));
stb.Children.Add(daY);
Resources.Add("StartOfPage", stb);
dgCustList.Columns.Add(new DataGridTextColumn
{
Header = "ID",
Binding = new Binding("CustomerID")
});
dgCustList.Columns.Add(new DataGridTextColumn
{
Header = "Müşteri Ad",
Binding = new Binding("CustomerName")
});
dgCustList.Columns.Add(new DataGridTextColumn
{
Header = "Müşteri Soyad",
Binding = new Binding("CustomerSurname")
});
dgCustList.Columns.Add(new DataGridTextColumn
{
Header = "Müşteri Tel",
Binding = new Binding("CustomerPhone")
});
LoadGrid();
}
private void LoadGrid()
{
var client = new EczServiceClient();
client.CustomerInfoCompleted += client_CustomerInfoCompleted;
client.CustomerInfoAsync();
}
void client_CustomerInfoCompleted(object sender, CustomerInfoCompletedEventArgs e)
{
dgCustList.ItemsSource = e.Result;
}
private void dgCustList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var customer = dgCustList.SelectedItem as CustomerInfo;
if (customer == null) return;
var client = new EczServiceClient();
client.CustomerDetailCompleted += client_CustomerDetailCompleted;
client.CustomerDetailAsync(customer.CustomerID);
}
void client_CustomerDetailCompleted(object sender, CustomerDetailCompletedEventArgs e)
{
IN HERE I WANT TO FILL DATAGRID LIKE MASTER GRID BUT ITS NOT LET ME ( dgCustDetail.ItemSource = e.Result)
}
You did NOTdefine binding for you detail data grid and you set the autogenerate columns to false. You need to define the binding either in XAML or in code behind before the client_CustomerDetailCompleted event fires. Simply setting the item source alone won't work because the detail datagrid does not contain columns. This is where MVVM pattern comes in handy.
The following code in a WPF app creates a hyperlink that looks and acts like a hyperlink, but doesn't do anything when clicked.
What do I have to change so that when I click it, it opens the default browser and goes to the specified URL?
alt text http://www.deviantsart.com/upload/4fbnq2.png
XAML:
<Window x:Class="TestLink238492.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel Margin="10">
<ContentControl x:Name="MainArea"/>
</StackPanel>
</Window>
Code Behind:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace TestLink238492
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
FlowDocumentScrollViewer fdsv = new FlowDocumentScrollViewer();
FlowDocument doc = new FlowDocument();
fdsv.Document = doc;
fdsv.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
doc.PagePadding = new Thickness(0);
Paragraph paragraph = new Paragraph();
doc.Blocks.Add(paragraph);
Run run = new Run("this is flow document text and ");
paragraph.Inlines.Add(run);
Run run2 = new Run("this is a hyperlink");
Hyperlink hlink = new Hyperlink(run2);
hlink.NavigateUri = new Uri("http://www.google.com");
paragraph.Inlines.Add(hlink);
StackPanel sp = new StackPanel();
TextBlock tb = new TextBlock();
tb.Text = "this is textblock text";
sp.Children.Add(tb);
sp.Children.Add(fdsv);
MainArea.Content = sp;
}
}
}
I found the answer to this one, you have to add RequestNavigate and handle it yourself:
Run run2 = new Run("this is a hyperlink");
Hyperlink hlink = new Hyperlink(run2);
hlink.NavigateUri = new Uri("http://www.google.com");
hlink.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(hlink_RequestNavigate);
paragraph.Inlines.Add(hlink);
void hlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
Got the solutions for this Poma. The code section below should be added to your class where you need to do this. Or you can put it in a static class somewhere if you need to get to it from multiple files. I've tweaked it slightly for what I'm doing.
#region Activate Hyperlinks in the Rich Text box
//http://stackoverflow.com/questions/5465667/handle-all-hyperlinks-mouseenter-event-in-a-loaded-loose-flowdocument
void SubscribeToAllHyperlinks(FlowDocument flowDocument)
{
var hyperlinks = GetVisuals(flowDocument).OfType<Hyperlink>();
foreach (var link in hyperlinks)
link.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(link_RequestNavigate);
}
public static IEnumerable<DependencyObject> GetVisuals(DependencyObject root)
{
foreach (var child in LogicalTreeHelper.GetChildren(root).OfType<DependencyObject>())
{
yield return child;
foreach (var descendants in GetVisuals(child))
yield return descendants;
}
}
void link_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
//http://stackoverflow.com/questions/2288999/how-can-i-get-a-flowdocument-hyperlink-to-launch-browser-and-go-to-url-in-a-wpf
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
#endregion Activate Hyperlinks in the Rich Text box
You'll call it in your code like this:
string xaml = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(this.itemControl.NotificationItem.Body, true);
FlowDocument flowDocument = XamlReader.Load(new XmlTextReader(new StringReader(xaml))) as FlowDocument;
SubscribeToAllHyperlinks(flowDocument);
bodyFlowDocument.Document = flowDocument;
All the HTMLConverter stuff can be found at: http://blogs.msdn.com/b/wpfsdk/archive/2006/05/25/606317.aspx
That's if you need to convert HTML to a Flow Document. Although, that's slightly out of the scope of this topic.