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.
Related
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?
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.
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.
I just installed vs2013 and I carried a WPF project, previous developed with VS2010, under this environment. Happens to me a very strange thing that obviously does not happen with 2010 .... I have my own custom control that defines a runtime URI to a dictionary ... the strangeness lies in the fact that in the code of xaml page, when i define the use of my control, i've got the error reported in subject, but if i start the dubug, everything works fine and the style is correctly applied .... someone got an idea?
the code look like that :
the custom control
using System;
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;
using System.Windows.Data;
namespace myListView
{
public class myListView : ListView
{
// ... some dependency property
protected override void OnInitialized(EventArgs e)
{
Uri uri = new Uri("Skins\\myDictionary.xaml", UriKind.Relative);
dictionary = Application.LoadComponent(uri) as ResourceDictionary;
//... rest of code
base.OnInitialized(e);
}
}
}
the xaml page
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xlw="clr-namespace:myListView;assembly=myListView"
Title="MainWindow" Height="350" Width="525">
<Grid>
<xlw:myListView>
<!-- the editor tell me that can't find the Skins/myDictionary.xaml -->
</xlw:myListView>
</Grid>
</Window>
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));
}
}
}