"Help/Tutorial" page in WPF menu bar - wpf

I've added a menu bar to application, because I wanted to add there a little tutorial under "Help" menu. Like Help - > "How to use the program", that's the idea.
I would like this to be, a pop up window that has ability to include text, as well as photos to better explain the program. It should be able to have a little bit of formatting (just a simple spaces, tabulator, new lines) so the text with photos looks clean.
To sum up, I need an idea, a tutorial, a guide, an adivce - you name it what should I use to implement those things, because right now I am in a blank spot.
#edit Hope this clarifies my question.

To do this you need menu items:
<Window x:Class="WPFTestApp2.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>
<Menu>
<MenuItem Header="Help">
<MenuItem Header="How to use this program" Click="MenuItem_Click"></MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
And you would need a click handler:
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFTestApp2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
HelpWindow window = new HelpWindow();
window.Show();
}
}
}
Then, you would need to create a new window with all the help text you would like. You would need to design that window and I think that is really dependent on what the application does.

Related

Having an issue with a null exception on WPF DataGrid when trying to display data from a database with Linq [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
What does InitializeComponent() do, and how does it work in WPF?
(2 answers)
Closed 2 years ago.
Afternoon lads/lasses,
I'm trying to show data from a database table in a data grid but I'm getting a null exception.
Here's my code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using System.Xml.Linq;
namespace LinkToSQL
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
LinqtoSQLDataClassesDataContext dataContext;
public MainWindow()
{
string connectionString = ConfigurationManager.ConnectionStrings["LinkToSQL.Properties.Settings.MyFirstDBConnectionString"].ConnectionString;
dataContext = new LinqtoSQLDataClassesDataContext(connectionString);
InsertUniversity();
}
public void InsertUniversity()
{
University yale = new University();
yale.Name = "Yale";
dataContext.Universities.InsertOnSubmit(yale);
dataContext.SubmitChanges();
MainDataGrid.ItemsSource = dataContext.Universities;
}
}
}
When I run it, I get this exception which tells me that the dataGrid is null:
Here's my XAML:
<Window x:Class="LinkToSQL.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:LinkToSQL"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid x:Name="MainDataGrid" VerticalAlignment="Center" HorizontalAlignment="Center" Height="400" Width="760"/>
</Grid>
</Window>
Thanks in advance!
Call InitializeComponent() the first thing you do in the constructor, before you access any elements that you have defined in the XAML markup:
public MainWindow()
{
InitializeComponent();
string connectionString = ConfigurationManager.ConnectionStrings["LinkToSQL.Properties.Settings.MyFirstDBConnectionString"].ConnectionString;
dataContext = new LinqtoSQLDataClassesDataContext(connectionString);
InsertUniversity();
}
What does InitializeComponent() do, and how does it work in WPF?

CefSharp-WPF : Application is not rendered in other PC

I am new on CefSharp so I followed the first tutorial of CefSharp-WPF that was made me creating a new application.
However when I copied this application with folder in bin/x64/release directory to other pc, It opened the application but the page won't rendered.
Need I set something configs for launch other PC?
Here is my code snippet.
MainWindow.xaml.cs
using CefSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NetFwTypeLib;
namespace StocktalkPC
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
var settings = new CefSharp.CefSettings
{
};
settings.CefCommandLineArgs.Add("disable-gpu", "1");
Cef.Initialize(settings);
InitializeComponent();
}
}
}
MainWindow.xaml
<Window x:Class="StocktalkPC.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:StocktalkPC"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="1260">
<Grid>
<cefSharp:ChromiumWebBrowser Grid.Row="0"
Address="http://pc.dev.stocktalk.kr:30802/webchat" />
</Grid>
</Window>
It was address problem. I have added ti host as that domain but it is not real domain.

Show Partial video in MediaElement

I want to use the media element, yet display only part of the video rectangle.
for example : if the video is 100X100 px, i would like to show only the left half of the video, e.g the 50x100 px on the left.
You could place it in a ContentPresenter with negative Margins and ClipToBounds set to true, this will crop the video.
It can be done by setting the Clip property of MediaElement. You can set it to any PathGeometry, below is a simple example.
XAML:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="378" Width="472">
<Canvas>
<MediaElement LoadedBehavior="Play" Name="myME" Source="c:\\1.wmv" Width="320" Height="240" Canvas.Left="0" Canvas.Top="0">
</MediaElement>
</Canvas>
</Window>
C#
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace tests
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.MyMedia1.Clip =
new RectangleGeometry( new Rect(0, 0, myME.Width/3, myME.Height));
}
}
}

Silverlight: Does UserControl Have a Background Property?

I created a BarMenuItem UserControl in a Silverlight class library, and try to used in my main Silverlight application.
BarMenuItem.xaml:
<UserControl x:Class="ButtonControlLibrary.BarMenuItem"
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"
d:DesignHeight="300" d:DesignWidth="400">
</UserControl>
BarMenuItem.xmal.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace ButtonControlLibrary
{
public partial class BarMenuItem : UserControl
{
public BarMenuItem()
{
InitializeComponent();
}
}
}
So in the MainPage, I have xmlns:blib="clr-namespace:ButtonControlLibrary;assembly=ButtonControlLibrary" defined
Tried to use BarMenuItem:
<blib:BarMenuItem Width="100" Height="150" Background="Red"/>
Compile and run it, and I expected to see a Red background, but I see nothing.
What happenend?
I'm so confused. I googled a lot, finding there is a workaround, but very ugly: Put a Grid container inside the UserControl, and then binding its Background property to the UserControl's Background:
Background="{Binding Background, ElementName=guiUserControl}"
But this is not the solution. Please advice.
In Adobe Flex, it's so natural to change a UserControl's background like what I do in the above code.
Am I missing anything?
Thanks.
The property is there but it doesn't seem to work in Silverlight. Your next best bet is the solution you said you already know. Binding layoutroot's background with usercontrol's background.

Inheriting from a UserControl in WPF

I am fairly new to WPF and I am having a problem with inheriting from a user control.
I created a User Control and now I need to inherit from that control and add some more functionality.
Has anyone does this sort of thing before? Any help would be greatly appreciated.
Thank you
Well .. you create your base control
public abstract class BaseUserControl : UserControl{...}
then in the XAML file :
<Controls:BaseUserControl x:Class="Termo.Win.Controls.ChildControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Namespace.Of.Your.BaseControl">
And that should work.
EDIT: Hmm.. this example is useful when you have a base control without XAML and then inherit from it. The other way around(from a base control with Xaml) - I'm not sure how you can go about it.
EDIT2: Apparently from this post + comments i take that what you want might not be possible.
AFAIK you cannot inherit the xaml, you can only inherit the code behind.
We recently encountered the same problem on our project. The way we ended up solving our problem was to create a usercontrol and adding it to the "child" usercontrol.
If that doesnt work/help take a look at this:
https://web.archive.org/web/20200815091447/http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx[1]
I may have a bit of a solution: Composition instead of inheritance - I have come up with control, that has 'content slots' assignable from outside through databinding, look at my SO thread.
Example of use:
<UserControl ... >
<!-- My wrapping XAML -->
<Common:DialogControl>
<Common:DialogControl.Heading>
<!-- Slot for a string -->
</Common:DialogControl.Heading>
<Common:DialogControl.Control>
<!-- Concrete dialog's content goes here -->
</Common:DialogControl.Control>
<Common:DialogControl.Buttons>
<!-- Concrete dialog's buttons go here -->
</Common:DialogControl.Buttons>
</Common:DialogControl>
<!-- /My wrapping XAML -->
</UserControl>
Together with some handling code in codebehind it would be a nice base component for dialog windows.
You cannot inherit the xaml code it self. However creating an abstract class of the codebehind, will allow you to edit in code behind, from a derived class object.
Xaml Code: { Window1.xaml }
<Window
x:Class="WPFSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="auto" Width="256" Title="WPF Sameples">
<Grid>
<Button x:Name="Button1" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Click Me"/>
</Grid>
</Window>
CodeBehind: { Window1.xaml.cs }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFSamples
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public abstract partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}
Derived Class : { DisabledButtonWindow.cs }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WPFSamples
{
public sealed class DisabledButtonWindow : Window1
{
public DisabledButtonWindow()
{
Button1.IsEnabled = false;
}
}
}
although you cannot inherit from the wpf source it self, you are able to use this "Window1" control as a template for all other derived controls.
You can accomplish this by using a delegate.
Essentially, you need to create an interface (YourInterface) which wraps up the functionality you want, then make both the user control and your class implement that interface.
Next, make sure the user control has a reference to an object of type IYourInterface, so that when your user control attempts to invoke a method of the Interface, it calls your class' method.
Because both the user control and class implement the same interface, they can be seen as the same kind of object - meaning you can put them both into a collection of objects of type IYourInterface. This should give you the behavior you want.
In ASP.NET I use this technique often, by having my classes inherit from abstract class ancestors. I don't understand why WPF doesn't support this. :(
I think that you can do this but that you will have to redefine any functions and possibly some other stuff that you reference in the xaml in the child class.
IE if you have a button click event that you subscribe to in the base class xaml you will need override the button click in the child class and call the base class button click event.
Not one hundred percent sure of the the details since it's my coworkers code that I'm drawing from but thought this would give a start to anyone looking to implement this in the future.

Resources