Can't change DataContext WPF - c#

I am trying to use a MVVM pattern instead of putting all logic in the code-behind file, but I can't seem to get it working.
My view is this:
<Window x:Class="WpfApp4.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:WpfApp4"
mc:Ignorable="d"
xmlns:vm="clr-namespace:WpfApp4.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:ViewModelMain />
</Window.DataContext>
<Grid>
</Grid>
And this is my ViewModel:
namespace WpfApp4.ViewModel
{
class ViewModelMain
{
}
}
What am I doing wrong here?

Visual studio can be a bit temperamental sometimes with xaml warnings and errors. Try closing the window, and/or cleaning and rebuilding the project, or if all else fails close all windows and restart visual studio

Related

Audio but no video when playing a movie using mediaplayerelement in wpf

I am trying to use MediaPlayerElement in a WPF to play streaming videos. I have set up a simple WPF window that contains a MediaPlayerElement instance and auto-plays a video. When I run the program, I can hear audio but the video is not visible.
I started by following this tutorial:
https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/mediaplayerelement
Unfortunately the tutorial appears to be out of date, as it no longer works. I made a few corrections to get it to a running state, but cannot get the video to appear.
I have tried placing the MediaPlayerElement inside a Grid or StackPanel, but can't because it is not a UIElement.
<Window x:Class="MediaPlayerElement_Test.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:MediaPlayerElement_Test"
xmlns:controls="clr-namespace:Microsoft.Toolkit.Forms.UI.Controls;assembly=Microsoft.Toolkit.Forms.UI.Controls"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<controls:MediaPlayerElement Name="mediaPlayerElement" AutoScaleDimensions="800,450"
Source="https://mediaplatstorage1.blob.core.windows.net/windows-universal-samples-media/elephantsdream-clip-h264_sd-aac_eng-aac_spa-aac_eng_commentary-srt_eng-srt_por-srt_swe.mkv"
AutoPlay="True" Anchor="Top" Height="450" Width="800" AreTransportControlsEnabled="True" />
</Window>
Does anyone know what I am doing wrong here? How do I get the video to display in the window?
Try this one, this works perfectly for me
<Window x:Class="Test.Media"
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:Test"
mc:Ignorable="d"
Title="Media" Height="450" Width="800">
<Grid>
<MediaElement Margin="10,10,10,0 " Source="http://mediaplatstorage1.blob.core.windows.net/windows-universal-samples-media/elephantsdream-clip-h264_sd-aac_eng-aac_spa-aac_eng_commentary-srt_eng-srt_por-srt_swe.mkv"
Name="McMediaElement"
Width="450" Height="250" LoadedBehavior="Play" UnloadedBehavior="Stop" Stretch="Fill"/>
</Grid>
if this is not working then it might be the issue with your video codec

WPF - Update static resource value at runtime

I have code similar to the following:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Software_Suite_Maker"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" x:Class="WpfApplication1.App"
StartupUri="MainWindow.xaml">
<Application.Resources>
<FontFamily x:Key="FontFamilyName">./Fonts/#Segoe UI</FontFamily>
</Application.Resources>
and the Window xaml code is:
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox FontFamily="{StaticResource FontFamilyName}" Margin="135,122,187,180" Text="test"/>
<Button FontFamily="{StaticResource FontFamilyName}" Margin="135,144,329,154" Content="test"/>
</Grid>
Now I want to change the value of FontFamilyName from behind code. I wrote this code:
var font = TryFindResource("FontFamilyName") as FontFamily;
font = new FontFamily("./Fonts/#Tahoma");
But nothing happened and did not change.
My question is: How can I change FontFamilyName value from behind code and changes will also be made on the objects?
You have to use a DynamicResource for that :
<TextBox FontFamily="{DynamicResource FontFamilyName}" Margin="135,122,187,180"
Text="test"/>
<Button FontFamily="{DynamicResource FontFamilyName}" Margin="135,144,329,154"
Content="test"/>
Read on MSDN about DynamicResource:
Provides a value for any XAML property attribute by deferring that value to be a reference to a defined resource. Lookup behavior for that resource is analogous to run-time lookup.

Set window icon correctly and happening XamlParseException

I set window icon property in xaml code or in the properties window correctly in a very simple wpf application, but visual studio always throws an XamlParseException at runtime. Why is this happening?(using visual studio 2013 and windows 8, my icon size is 15 KB)
<Window x:Class="WpfApplication1.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" Icon="pack://siteoforigin:,,,/Resources/MyIcon.ico">
<Grid>
</Grid> </Window>
<Window x:Class="WpfApplication1.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" Icon="/Resources/MyIcon.ico">
<Grid>
</Grid> </Window>
It is a problem with your image Build Action Property, check the arrows for further info..

Looking to find how to add resize button

Hello:
I have been experimenting with WPF this afternoon, Im wondering does anyone know how I can manually enable resizing ? I have my main windows set with the following properties which removes the close/min/max and resize;
<Window x:Class="WpfApplication1.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" AllowsTransparency="True"
WindowStyle="None" Background="Maroon" ResizeMode="CanResize">
</Window>
I have found how to add my own close/min/max buttons but have not found how to enable resizing.
ResizeMode="CanResizeWithGrip"

How to instantiate DataContext object in XAML

I want to be able to create an instance of the DataContext object for my WPF StartupUri window in XAML, as opposed to creating it code and then setting the DataContext property programmaticly.
The main reason is I don't need to access the object created externally and I don't want to have to write code behind just for setting the DataContext.
I'm sure I've read somewhere how to instantiate the DataContext object in XAML but I can't find it in any of the usual places...
You add an XML namespace for whatever namespace your DataContext lives in, create an instance of it in the Window Resources and set the DataContext to that resource:
<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<local:MyViewModel x:Key="MyViewModel"/>
</Window.Resources>
<Grid DataContext="{StaticResource MyViewModel}">
</Grid>
</Window>
You can just specify this directly in XAML for the entire Window:
<Window
... xmlns definitions ...
>
<Window.DataContext>
<local:CustomViewModel />
</Window.DataContext>
</Window>
This creates a view model named "CustomViewModel" in the namespace aliased to local, directly as the DataContext for the Window.
Assuming this code:
public abstract class BaseView { }
public class RuntimeView : BaseView { }
public class DesigntimeView : BaseView { }
Try this:
<Page.DataContext>
<local:RuntimeView />
</Page.DataContext>
<d:Page.DataContext>
<local:DesigntimeView />
</d:Page.DataContext>
<ListBox ItemsSource="{Binding}" />
Best of luck!
If you need to set the DataContext as same control class:
<Window x:Class="TabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TabControl"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
</Window>
use RelativeSource binding.
or just
<Window x:Class="TabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TabControl"
Title="MainWindow" Height="350" Width="525"
>
<Window.DataContext>
< new instance of any viewModel here....>
</Window.DataContext>
</Window>
If want to assign an instance of different class than itself.

Categories

Resources