How to add UserControl to a Panel on a WPF Window - wpf

I think I'm missing something that should be obvious here, but I'm drawing a blank on this one.
I've built a very primitive UserControl containing nothing more than a TextBox to use as a log window:
<UserControl x:Class="My.LoggerControl"
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"
x:Name="LoggerView">
<Grid x:Name="LayoutRoot">
<TextBox x:Name="LogWindow" AcceptsReturn="True"/>
</Grid>
</UserControl>
I don't expect that to be the best way to do it, but it should be good enough for a prototype.
The code-behind is similarly simple:
public partial class LoggerControl : UserControl, ILogger
{
public LoggerControl()
{
InitializeComponent();
}
private LogLevel level = LogLevel.Warning;
#region ILogger
public LogLevel Level
{
get { return level; }
set { level = value; }
}
public void OnError(string s)
{
if (level >= LogLevel.Error)
LogWindow.AppendText("ERROR:::" + s + "\n");
}
// ...
#endregion
}
The thing I can't figure out is how to add this control to my MainWindow.xaml. Simplifying, lets say my window looks like this:
<Window x:Class="My.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:My"
Title="Test" Height="350" Width="525">
<Grid>
<local:LoggerControl x:Name="LogView" />
</Grid>
</Window>
Even with something so simple, the Designer in Visual Studio 2010 can't load the main window. The error given is:
A value of type 'LoggerControl' cannot be added to a collectionor dictionary of type 'UIElementCollection'.
This error message has only one unrelated hit in the major search engines (plus duplicates) so I haven't found any useful help. Microsoft's own documentation seems to imply that this should work.
Any idea how to solve this?

<UserControl x:Class="My.LoggerControl"
xmlns:local="clr-namespace:My.LogTest"
Looks like you may have made a mistake in the namespacing? LoggerControl is listed as being the namespace My, while you're importing My.LogTest and assigning it to the xml-prefix local. Change this to:
xmlns:local="clr-namespace:My"
And I think it should work. Otherwise, fix the LoggerControl declaration.

Related

C# WPF XAML Mouse(Down) events not working anymore as of .NET 5(C#9)

Screen shot of the issue I'm running into after going to .NET 5 (C# 9)
I'm trying to figure out how they want us to implement this simple code now.
It says its an error but it still builds the project. Although, this does prevents designer from building the view from xaml, so unable to edit the xaml properly, also prevents the functionality, in this case trying to drag a window with mouse_down event.
XAML:
<Window x:Class="WPFProject.Window1"
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:WPFProject"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Border MouseDown="CodeBehind_DragFunction">
<TextBlock Text="Hello World" Foreground="Black"/>
</Border>
</Window>
C# Code-behind
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
void CodeBehind_DragFunction(object sender_, MouseButtonEventArgs e_)
{
DragMove();
}
}
This worked in previous versions..
Suggested answer posted by #Pharaz Fadaei
It was an IDE bug that was fixed by following these steps outlined in this post: The event 'foo' is not a RoutedEvent

XAML.Parse.Exception on WPF project's deployment

I'm having a problem when I deploy my WPF project, the deployed project crashes on startup and produces a XAML.Parse.Exception with an inner exception of "Cannot have nested BeginInit calls on the same instance" at Line 4 Position 70. The App has full permissions on my computer. I am asking this question because the few questions asked about this had no real solution to the problem.
Here is the XAML code it is referencing with the first couple of lines.
<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" x:Class="ASRV.MainWindow"
Title="ASRV GUI" Height="768" Width="1024" ResizeMode="CanMinimize">
<Window.Resources>
</Window.Resources>
<Window.Background>
<ImageBrush ImageSource="pack://siteoforigin:,,,/background.png"/>
</Window.Background>
My guess is the reason being:
<Window.Background>
<ImageBrush ImageSource="pack://siteoforigin:,,,/background.png"/>
</Window.Background>
You might be reusing this image elsewhere in the same window or subcontrol.
BeginInit is called in databinding and this is the only databound thing I could see in your sample code. "BeginInit calls on the same instance" points to it being bound twice.
This error is encountered whenever an object is attempted to be bound twice, as #basarat says in his answer. Further to the example seen in the OP, I came across this same error caused in my case by setting a DataContext in the CodeBehind as well as in the xaml:
In the MainWindow.xaml file, I had the following:
<Window xmlns:vm="clr-namespace:MyApp.ViewModel"
[...more xmlns references skipped for brevity...]
<!--the next three lines caused a problem, when combined with the code behind-->
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
</Window>
And in the MainWindow.xaml.cs file, I had this:
namespace MyApp.View
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainViewModel MainViewModel { get; set; }
public MainWindow()
{
bool success = false;
MainViewModel = new MainViewModel();
// This line caused the problem in combination with the xaml above
DataContext = MainViewModel;
InitializeComponent();
}
}
}
In order to get rid of the problem, I deleted one of the DataContext setters; it works with either the data context set in xaml or the data cantext set in the code behind, not both.
P.S. I add this answer because this is the first answer to come up on searches for this problem and I felt that further explanation would be useful to other visitors.

Setting Window.Datacontext

I am trying to use my viewmodel as my window's datacontext but am getting the error:
ViewModel is not supported in a Windows Presentation Foundation (WPF) project.
Clearly, I am not understanding something about the syntax and databinding my window to my view model, but I am not sure what it is that I don't know.
Any advice on what I should be reading?
<Window x:Class="SunnyBeam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SunnyBeam" Height="488.358" Width="1014.552">
<Window.DataContext>
<ViewModel/>
</Window.DataContext>
<Grid>
</Grid>
</Window>
Usually I set DataContext through codebehind like that:
public partial class Flor1 : Window
{
public Flor1()
{
var dc = new MyViewModel();
dc.LoadData();
DataContext = dc;
InitializeComponent();
}
}
In place of MyViewModel may be anything you want to bind to.
define class like
public class ViewModel
{
public string Name { get; set; }
public ViewModel()
{
}
}
Use it in xaml like
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<ui:ViewModel/>
</Window.DataContext>
<Grid>
</Grid>
It should work.
I thought I would throw in my experience with this error.
I had my datacontext setup same as below, and kept getting an error that ViewModel didn't exist, which I know it did. I refused to set it in code behind, simply rebuilding my project actually fixed this error.
<Window.DataContext>
<ViewModel/>
</Window.DataContext>

How to Use RadWindow as MainWindow of WPF application

I want to use RadWindow as the main window of my WPF application. So that outer window also take the theme of overall application. I used this approach, but after that application is not being shown on Taskbar anymore. After reading different threads I came to know that because RadWindow is not child of Window so that's why its not possible.
So What is now I am trying to do, is to hide the outer Window completely somehow and to use RadWindow as a child, but parent of all other controls. Following is XAML
<Window x:Class="MyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
WindowStyle="None"
Title="MainWindow" >
<telerik:RadWindow WindowStartupLocation="CenterScreen" Width="Auto" Height="Auto" x:Name="MyRadWindow">
<Grid>
<!-- All Controls here -->
</Grid>
</telerik:RadWindow>
</Window>
But I am unable to completely hide the outer window. It still shows the borders. Then as a second step I have to handle the minimize, maximize and window close events from this RadWidow.
If anyone have tried this approach please help me out or suggest what would be the better way of doing this?
The main purpose of doing all this is to Sync the GUI of Outerwindow with the current TelerikTheme.
There's a simpler solution using the RadWindowInteropHelper like this:
using Telerik.Windows.Controls.Navigation;
public partial class MyRadWindow : RadWindow
{
public MyRadWindow()
{
InitializeComponent();
RadWindowInteropHelper.SetShowInTaskbar(this, true);
}
}
I think you should try to set main class as telerik window instead of nesting inside normal window:
<telerik:RadWindow x:Class="MyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
WindowStyle="None" WindowStartupLocation="CenterScreen" ShowInTaskbar="True"
Title="MainWindow" >
<Grid>
<!-- All Controls here -->
</Grid>
</telerik:RadWindow>
Don't forget to change base class of MyTest.MainWindow to RadWindow
EDIT: Sorry didn't notice provided link.
You can try hide main window with overriding it style by setting following attributes:
WindowStyle="None" Background="Transparent" AllowsTransparency ="True"
First open the MainWindow.xaml file and replace the Window declaration with RadWindow declaration:
<telerik:RadWindow x:Class="RadWindowAsMainWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Loaded="RadWindow_Loaded_1"
Header="MainWindow" Height="350" Width="525">
...
</telerik:RadWindow>
and in code-behind:
`
public partial class MainWindow : RadWindow
{
...
}
2. Then override OnStartup method of the Application class to show the RadWindow:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
new MainWindow().Show();
base.OnStartup(e);
}
}
`
Then in RadWindowLoad event write this:
`
private void RadWindow_Loaded_1(object sender, RoutedEventArgs e)
{
var window = this.ParentOfType();
window.ShowInTaskbar = true;
}
`

Access XAML object from code

I created a UserControl in WPF:
In Xaml:
<UserControl x:Class="OutlookPanel.MailRelation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300"
xmlns:graph="clr-namespace:MyPanel"
>
<DockPanel>
<graph:Graph Name="theGraph" NodesBindingPath="ChildNodes"
NodeTemplateSelector="{StaticResource nodeTemplateSelector}">
..
</DockPanel>
</UserControl>
I cs:
object theThing = e.Parameter;
((MailRelation)sender).theGraph.CenterObject = theThing;
This last sentence does not work as theGraph is not accessible.
Any idea why i can access theGraph ?
Thanks
John
Name="theGraph"
should be
x:Name="theGraph"
from http://msdn.microsoft.com/en-us/library/ms752059.aspx
x:Name: Specifies a run-time object
name for the instance that exists in
run-time code after an object element
is processed. You use x:Name for cases
of naming elements where the
equivalent WPF framework-level Name
property is not supported. This
happens in certain animation
scenarios.
Probably because that property is private. Provide a public getter and you should be able to get it. In your code, add something like
public Graph TheGraph { get { return theGraph; } }

Resources