synchronize data template in a list box with a button - c#

<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" />

Related

Stretch Usercontrol in Dockpanel

I'm new at WPF. I know from Forms that i can add usercontrols to a panel. How can I do this in WPF? I tried a Grid, DockPanel and StackPanel but I don't know how can I stretch my usercontrol? In this Grid oder what else will only be this usercontrol.
I need to switch the content of the grid or else because I want to display different usercontrols.
Main XAML
<Window x:Class="TaxHelper.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:entites="clr-namespace:TaxHelper.Entity"
xmlns:local="clr-namespace:TaxHelper"
xmlns:properties="clr-namespace:TaxHelper.Properties"
mc:Ignorable="d"
Title="TaxHelper" Height="558" Width="803">
<Window.Resources>
<local:InvoiceStatusIconConverter x:Key="IconConverter"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="251*"/>
<ColumnDefinition Width="292*"/>
</Grid.ColumnDefinitions>
<DockPanel LastChildFill="True">
<Menu DockPanel.Dock="Top" Height="20" Margin="0,0,10,0">
<MenuItem Header="Datebank" Height="20">
<MenuItem Header="Importieren" Name="miImport" Click="miImport_Click"/>
</MenuItem>
<MenuItem Header="Daten" Height="20">
<MenuItem Header="Laden" Name="miLoadData" Click="miLoadData_Click"/>
</MenuItem>
<MenuItem Header="Tests" Height="20">
<MenuItem Header="Add Usercontrol" Name="miTestbutton" Click="miTestbutton_Click"/>
</MenuItem>
</Menu>
<StackPanel></StackPanel>
</DockPanel>
<TreeView x:Name="tvNavigation" Margin="10,26,10,10" HorizontalContentAlignment="Stretch" TreeViewItem.Expanded="tvNavigation_Expanded">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type entites:Owner}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type entites:EconomyUnit}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type entites:Report}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type entites:ReportItem}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type entites:Invoice}" ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Status, Converter={StaticResource IconConverter}}" MaxHeight="16px" MaxWidth="16px" HorizontalAlignment="Left"></Image>
<TextBlock Text="{Binding Company}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
<!--<StackPanel x:Name="dpContent" Orientation="Vertical" HorizontalAlignment="Stretch" Height="507" Margin="10,10,10,0" VerticalAlignment="Top" Width="408" Grid.Column="1"/>-->
<Grid Height="Auto" Name="dpContent" Width="Auto" Grid.Column="1" Margin="24,26,29,10">
</Grid>
</Grid>
</Window>
Usercontrol:
<UserControl x:Class="TaxHelper.Invoice"
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:TaxHelper"
xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib"
mc:Ignorable="d" Height="152.438" Width="132.531">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="81*"/>
<RowDefinition Height="14*"/>
</Grid.RowDefinitions>
<mpp:MoonPdfPanel Background="LightGray" ViewType="SinglePage" PageRowDisplay="ContinuousPageRows" PageMargin="0,2,4,2" AllowDrop="True" x:Name="mpp" x:FieldModifier="private"/>
<StackPanel Margin="0,1,0,0" Grid.Row="1">
<Button Content="Exit" Click="Button_Click" Height="12" FontSize="5" />
</StackPanel>
</Grid>
</UserControl>
Add Usercontrol:
private void miTestbutton_Click(object sender, RoutedEventArgs e)
{
Invoice invoice = new Invoice();
invoice.HorizontalAlignment = HorizontalAlignment.Stretch;
invoice.VerticalAlignment = VerticalAlignment.Stretch;
dpContent.Children.Add(invoice);
}
The declaration of the UserContol is setting hard coded values for Width and Height, which means that the control cannot resize itself.
If you replace those two attributes with d:DesignWidth and d:DesignHeight then those values become "design-time only" values, so those values will be used when you view the control in the Visual Studio designer, but not by the application at run-time...
<UserControl x:Class="TaxHelper.Invoice"
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:TaxHelper"
xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib"
mc:Ignorable="d" d:DesignHeight="152.438" d:DesignWidth="132.531">

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.

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!

Observable Collection to Tabcontrol binding

<TabControl HorizontalAlignment="Stretch" TabStripPlacement="Left" Margin="10,20,0,0" Name="tabControl2" ItemsSource="{Binding NotesObs}" VerticalAlignment="Stretch" MinHeight="80">
<TabItem Header="Contract Read and understood, proposal letter read and understood." Name="tabItem2" FontSize="14" IsEnabled="True">
<Grid>
<Border Name="b_desc"/>
<TextBox HorizontalAlignment="Stretch" Margin="0" Name="textBox5" Text="{Binding ContractText}"
VerticalAlignment="Stretch" FontSize="12" TextWrapping="Wrap"
AutoWordSelection="True" VerticalScrollBarVisibility="Auto"
AcceptsReturn="True"
Width="{Binding ElementName=b_desc, Path=ActualWidth}"
Height="{Binding ElementName=b_desc, Path=ActualHeight}"
MaxWidth="{Binding ElementName=b_desc, Path=Width}"
MaxHeight="{Binding ElementName=b_desc, Path=Height}" />
</Grid>
</TabItem>
</TabControl>
Above is my Xaml and the name of my ObsCollection is
public ObservableCollection<NotesDisplay> NotesObs { get; set; }
I have declared properly the ContractText column in the NotesDisplay Model. I cant get this to bind for some reason but this works in a datagrid. Can someone explain what I am doing wrong in my XAML?
Thanks
Edit: I have viewed the linked solution but tabs arent showing up when I run the program. They do showup in the display in xaml
<UserControl x:Class="CAT_Application_WPF.UI.NotesPage"
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:CAT_Application_WPF.UI"
xmlns:viewModel="clr-namespace:CAT_Application_WPF.ViewModel"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
mc:Ignorable="d"
xmlns:oxy="http://oxyplot.org/wpf"
d:DesignHeight="640">
<Grid Margin="0,0,0,0" Background="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" d:DataContext="{d:DesignInstance viewModel:NotesPageViewModel}" >
<Grid.RowDefinitions>
<RowDefinition Height="60*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"></ColumnDefinition>
<ColumnDefinition Width="400"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TabControl HorizontalAlignment="Stretch" ItemsSource="{Binding NotesObs}" TabStripPlacement="Top" x:Name="_tabControl" VerticalAlignment="Stretch" MinHeight="80" Grid.ColumnSpan="2" Margin="121,28,279,-28">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock Text="{Binding ContractText}"/>
</TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBlock>
This is <TextBlock Text="{Binding EMSText}"/>
</TextBlock>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
</UserControl>
Now it's quite ok but you must remove your TextBlock inside another TextBlock stuff.
You can change
<TextBlock>
<TextBlock Text="{Binding ContractText}"/>
</TextBlock>
to
<TextBlock Text="{Binding ContractText}"/>
And
<TextBlock>
This is <TextBlock Text="{Binding EMSText}"/>
</TextBlock>
to something similar to
<StackPanel Orientation="Horizontal">
<TextBlock>This is</TextBlock>
<TextBlock Text="{Binding EMSText}"/>
</StackPanel>

Can't put a window in a style

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>

Categories

Resources