Can't put a window in a style - c#

I made an mvvm project in WPF.
However, I get an error in my MainWindow.xaml, saying I can't put a window in a style...
This is my MainWindow.xaml:
<Window x:Class="DataValidation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:BankEn.viewmodel"
xmlns:v="clr-namespace:BankEn.view"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:AccountVM/>
</Window.DataContext>
<Window.Resources>
<DataTemplate DataType="{x:Type vm:AccountVM}">
<v:HomePage/>
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding CurrentPage}"/>
</Grid>
</Window>
the error occurs on <v: HomePage>
In my HomePage.xaml I don't use the <window> tags ... So I have no idea what the actual problem could be:
<UserControl x:Class="BankEn.view.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:BankEn.viewmodel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:rules="clr-namespace:BankEn.Model.rules"
mc:Ignorable="d"
d:DesignHeight="300" Width="528">
<UserControl.DataContext>
<vm:AccountVM/>
</UserControl.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListBox Margin="8" ItemsSource="{Binding Accounts}" SelectedItem="{Binding SelectedAcc}" DisplayMemberPath="AccountHolder"/>
<StackPanel Margin="8" Grid.Column="1">
<Label Content="Accountholder:"/>
<Label FontWeight="Bold" FontSize="10" Height="25px" Content="{Binding SelectedAcc.AccountHolder}"/>
<Label Content="Accountnumber:"/>
<Label FontWeight="Bold" FontSize="10" Height="25px" Content="{Binding SelectedAcc.AccountNumber}"/>
<Label Content="Balance:"/>
<Label FontWeight="Bold" FontSize="10" Height="25px" Content="{Binding SelectedAcc.Balance}"/>
<Label Content="Transfer to:"/>
<ComboBox Name="cboReceiver" SelectedItem="{Binding CboAcc}" ItemsSource="{Binding Path=Accounts}" DisplayMemberPath="AccountHolder" SelectedValuePath="AccountHolder" />
<Label Content="Amount:"/>
<TextBox InputScope="Number" Name="Amount"></TextBox>
<StackPanel Orientation="Horizontal" Margin="0,16,0,0">
<Button Content="Transfer money" Command="{Binding UpdateAccount}" CommandParameter="{Binding Text, ElementName=Amount}" Width="250"/>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>

Related

What's the difference in this example between using DataContext in XAML & code behind?

I'm learning binding ways in WPF with DataContext, but I cannot understand why it runs like that.
Here are the codes below.
Ex1: can get correct binding datas (assign DataContext in code behind)
Xaml
<Window x:Class="DemoDataContextAndItemSource.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:DemoDataContextAndItemSource"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel Margin="15">
<WrapPanel>
<TextBlock Text="Window title: " />
<TextBox Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" Width="150" />
</WrapPanel>
<WrapPanel Margin="0,10,0,0">
<TextBlock Text="Window dimensions: " />
<TextBox Text="{Binding Width}" Width="50" />
<TextBlock Text=" x " />
<TextBox Text="{Binding Height}" Width="50" />
</WrapPanel>
</StackPanel>
</Window>
cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
Compile Success
And Ex2: why the Width and the Height number are NaN & Compile failed ?? (use DataContext in Xaml)
<Window x:Class="DemoDataContextAndItemSource.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:DemoDataContextAndItemSource"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainWindow/>
</Window.DataContext>
<StackPanel Margin="15">
<WrapPanel>
<TextBlock Text="Window title: " />
<TextBox Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" Width="150" />
</WrapPanel>
<WrapPanel Margin="0,10,0,0">
<TextBlock Text="Window dimensions: " />
<TextBox Text="{Binding Width}" Width="50" />
<TextBlock Text=" x " />
<TextBox Text="{Binding Height}" Width="50" />
</WrapPanel>
</StackPanel>
</Window>
Compile Failed
The expression
<Window.DataContext>
<local:MainWindow/>
</Window.DataContext>
would create a new MainWindow instance and assign it to the DataContext property.
In order to assign the current instance, you would have to write
<Window ... DataContext="{Binding RelativeSource={RelativeSource Self}}">

Keep getting error on x:DataType

I have been getting "the attribute 'DataType' from the XAML namespace is not Defined.
[the attribute 'DataType' from the XAML namespace is not Defined][1]
<Page
x:Class="VisitorApp.sample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:VisitorApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data="using:VisitorApp.Common"
mc:Ignorable="d">
<Page.Resources>
<DataTemplate x:Key="GuestListDataTemplate" x:DataType="data:GuestGlobal">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<StackPanel Margin="20,20,0,0">
<TextBlock Text="{Binding GuestName}" HorizontalAlignment="Left" FontSize="16" />
<TextBlock Text="{x:Bind HostName}" HorizontalAlignment="Left" FontSize="10" />
</StackPanel>
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,20,20,0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="1"
Name="ResultTextBlock"
FontSize="24"
Foreground="Red"
FontWeight="Bold"
Margin="20,20,0,0" />
<GridView ItemsSource ="{x:Bind Guests}"
ItemClick="ListView_ItemClick"
IsItemClickEnabled="True"
ItemTemplate="{StaticResource GuestListDataTemplate}">
</GridView>
</Grid>
</Page>
strong text
This helped me https://msdn.microsoft.com/en-us/magazine/mt683802.
Thanks All for your contribution.

synchronize data template in a list box with a button

<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="740">
<Window.Resources>
<local:Leute x:Key="freunde"/>
<DataTemplate x:Key="detail">
<StackPanel FlowDirection="LeftToRight" Orientation="Horizontal">
<TextBlock x:Name="tb2" Text="{Binding Path=Vorname}"></TextBlock>
<TextBlock Text="{Binding Path=Nachname}"></TextBlock>
<TextBlock Text="{Binding Path=Geburtsdatum, StringFormat = dd.MM.yy}"></TextBlock>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid DataContext="{StaticResource freunde}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox ItemTemplate="{StaticResource detail}" ItemsSource="{StaticResource freunde}"/>
<TextBlock Grid.Column="1" Text="Vorname" Margin="0,23,254,265"></TextBlock>
<TextBlock Grid.Column="1" Text="Nachname" Margin="0,72,290,221"></TextBlock>
<TextBlock Grid.Column="1" Text="Geburtsdatum" Margin="0,121,254,162" RenderTransformOrigin="0.086,1.118"></TextBlock>
<TextBox Grid.Column="1" Text="{Binding Path=Vorname}" Margin="122,28,110,261"/>
<TextBox Grid.Column="1" Text="{Binding Path=Nachname}" Margin="122,71,110,221"/>
<TextBox Grid.Column="1" Text="{Binding Path=Geburtsdatum, StringFormat = dd.MM.yy}" Margin="122,120,110,162"/>
</Grid>
Hello,
im learning for a test, an this was one task of it. If i select an Item in the Listbox it wont show in the Textbox. Tried some things like element name and so on but it didnt work out. Could you help me?
Solved it
IsSynchronizedWithCurrentItem="true"
was missing here :
<ListBox ItemTemplate="{StaticResource detail}" ItemsSource="{StaticResource freunde}" IsSynchronizedWithCurrentItem="True" x:Name="list" />

how to bind the Textbox outside the ItemControl

Is it possible to bind the textbox outside the itemssource of itemcontrol.
I Just start coding wpf so I don't have any idea.
This is my code:
<Page x:Class="InformationSys.View.Page1"
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"
xmlns:local="clr-namespace:InformationSys.View"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1"
DataContext="{Binding Information, Source={StaticResource Locator}}">
<ItemsControl ItemsSource="{Binding Gender}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".4*"/>
<ColumnDefinition Width=".3*" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox IsChecked="{Binding IsChecked}" FlowDirection="RightToLeft" Grid.Column="0" VerticalAlignment="Bottom">
<TextBlock Text="{Binding Gen_Type}"/>
</CheckBox>
<TextBox x:Name="textBox5" Grid.Column="1" Grid.Row="4" Margin="20 0 0 0" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Page>
Thanks!

How to bind Image coordinates on Grid

I have an Image(named car1) on grid and i want to bind car1 coordinates to textblock.
<Canvas>
<Image Name="car1" />
</Canvas>
<WrapPanel>
<TextBlock Text="" Margin="10" Name="xx" Width="50"></TextBlock>
<TextBlock Text="" Margin="10" Name="yy" Width="50">0</TextBlock>
</WrapPanel>
How to solve it ?
try this
<Window x:Class="WpfApplication2.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>
<Canvas>
<Image Name="car1" Canvas.Left="40" Canvas.Top="30"/>
</Canvas>
<WrapPanel>
<TextBlock Text="{Binding Path=(Canvas.Left), ElementName=car1}" Margin="10" Name="Left" Width="50"/>
<TextBlock Text="{Binding Path=(Canvas.Top), ElementName=car1}" Margin="10" Name="Top" Width="50"/>
</WrapPanel>
</Grid>
</Window>

Categories

Resources