Binding multiple unique UserControls in a ListView to an ObservableCollection
Tag : chash , By : barefootChild
Date : March 29 2020, 07:55 AM
it fixes the issue Look, you're overcomplicating it too much. All you need is an ObservableCollection that can hold all your items and a proper DataTemplate for each. There's no need for DataTemplateSelectors or any other stuff like that. Also, there's no need to point directly back to the ObservableCollection, whatever that means: MainWindow: <Window x:Class="WpfApplication5.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication5"
Title="Window3" Height="300" Width="300">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Item1}">
<local:UserControl1/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Item2}">
<local:UserControl2/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Item3}">
<local:UserControl3/>
</DataTemplate>
</Window.Resources>
<ListBox ItemsSource="{Binding}"/>
</Window>
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.ComponentModel;
namespace WpfApplication5
{
public partial class Window3 : Window
{
public Window3()
{
InitializeComponent();
DataContext = new ObservableCollection<ItemBase>
{
new Item1() {MyText1 = "This is MyText1 inside an Item1"},
new Item2() {MyText2 = "This is MyText2 inside an Item2"},
new Item3() {MyText3 = "This is MyText3 inside an Item3", MyBool = true}
};
}
}
public class ItemBase: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChange(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Item1: ItemBase
{
private string _myText1;
public string MyText1
{
get { return _myText1; }
set
{
_myText1 = value;
NotifyPropertyChange("MyText1");
}
}
}
public class Item2: ItemBase
{
private string _myText2;
public string MyText2
{
get { return _myText2; }
set
{
_myText2 = value;
NotifyPropertyChange("MyText2");
}
}
private ObservableCollection<string> _options;
public ObservableCollection<string> Options
{
get { return _options ?? (_options = new ObservableCollection<string>()); }
}
public Item2()
{
Options.Add("Option1");
Options.Add("Option2");
Options.Add("Option3");
Options.Add("Option4");
}
}
public class Item3: ItemBase
{
private string _myText3;
public string MyText3
{
get { return _myText3; }
set
{
_myText3 = value;
NotifyPropertyChange("MyText3");
}
}
private bool _myBool;
public bool MyBool
{
get { return _myBool; }
set
{
_myBool = value;
NotifyPropertyChange("MyBool");
}
}
}
}
<UserControl x:Class="WpfApplication5.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Border BorderBrush="Black" BorderThickness="1">
<StackPanel>
<TextBlock Text="This is UserControl1"/>
<TextBlock Text="{Binding MyText1}"/>
</StackPanel>
</Border>
</UserControl>
<UserControl x:Class="WpfApplication5.UserControl2"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Border BorderBrush="Black" BorderThickness="1">
<StackPanel>
<TextBlock Text="This is UserControl2"/>
<TextBox Text="{Binding MyText2}"/>
<ComboBox ItemsSource="{Binding Options}" SelectedItem="{Binding MyText2}"/>
</StackPanel>
</Border>
</UserControl>
<UserControl x:Class="WpfApplication5.UserControl3"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Border BorderBrush="Black" BorderThickness="1">
<StackPanel>
<TextBlock Text="This is UserControl3"/>
<TextBlock Text="{Binding MyText3}"/>
<CheckBox Content="This is the MyBool Property" IsChecked="{Binding MyBool}"/>
</StackPanel>
</Border>
</UserControl>
|
WPF binding multiple UserControls to a listbox
Tag : chash , By : user109285
Date : March 29 2020, 07:55 AM
Does that help I have multiple UserControls and i want to show them on a ListBox Lets assume i have an Employee abstract UserControl and i have 2 or more UserControls than use that Employee like AdministrativeEmployee and FactoryEmployee for each of those Employees i have diffrent data but the UserControl is very similar (same size, almost same fields), on the ViewModel side i have an abstract EmployeeViewModel, the AdministrativeEmployeeViewModel and the FatoryEmployeeViewModel, on the EmployeesView i have the ListBox with the databinding and on the EmployeesViewModel i have an ICollection Employees , In ListBox resources define two DataTemplates: <ListBox ItemsSource="{Binding Employees}">
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:AdministrativeEmployee}">
<local:AdministrativeEmployeeView />
</DataTemplate>
<DataTemplate DataType="{x:Type local:FactoryEmployee}">
<local:FactoryEmployeeView />
</DataTemplate>
</ListBox.Resources>
</ListBox>
public class Employee
{
public string Name { get; set; }
}
public class AdministrativeEmployee : Employee
{
}
public class FactoryEmployee : Employee
{
}
List<Employee> _source = new List<Employee>();
_source.Add(new AdministrativeEmployee { Name = "A test1" });
_source.Add(new FactoryEmployee { Name = "F test1" });
_source.Add(new AdministrativeEmployee { Name = "A test2" });
_source.Add(new FactoryEmployee { Name = "F test2" });
Employees = _source;
<UserControl ...>
<Grid>
<TextBlock Text="{Binding Name}" Background="Red" />
</Grid>
</UserControl>
<UserControl ...>
<Grid>
<TextBlock Text="{Binding Name}" Background="Green" />
</Grid>
</UserControl>
|
Binding control in style to usercontrols property
Tag : chash , By : dantino
Date : March 29 2020, 07:55 AM
I wish this help you Ok, for everybody interested, here is my solution. After a time of experementing i found out, that it is working, when the brush is defined as Resource of the UserControl <UserControl x:Class="Controls.wTextBox"
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:Controls"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignHeight="20" d:DesignWidth="150" Height="20" Width="150" BorderThickness="1" BorderBrush="LightGray" x:Name="PART_UserControl" Background="White">
<UserControl.Resources>
<VisualBrush x:Key="HintTextBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Foreground="DarkGray" Content="{Binding ElementName=PART_UserControl, Path=HintText}"/>
</VisualBrush.Visual>
</VisualBrush>
<SolidColorBrush x:Key="ClearHinTextBrush" Color="White"/>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" BorderBrush="Transparent" BorderThickness="0" Name="PART_TextBox">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="{Binding Source={StaticResource HintTextBrush}}"/>
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{Binding Source={StaticResource HintTextBrush}}"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="{Binding Source={StaticResource ClearHinTextBrush}}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<Button Grid.Column="1" Click="OnButtonClick" Width="18" BorderBrush="Transparent" BorderThickness="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" Focusable="False">
<Button.Content>
<Path Stretch="Fill" Fill="#000000" Margin="2" Data="M29.898 26.5722l-4.3921 0c-0.0118,-0.635 -0.0177,-1.0172 -0.0177,-1.1583 0,-1.4229 0.2352,-2.5929 0.7056,-3.5102 0.4704,-0.9231 1.417,-1.952 2.8281,-3.1044 1.4111,-1.1465 2.2578,-1.8991 2.5282,-2.2578 0.4292,-0.5585 0.6409,-1.1818 0.6409,-1.8579 0,-0.9408 -0.3763,-1.7463 -1.1289,-2.4224 -0.7526,-0.6703 -1.7639,-1.0054 -3.0397,-1.0054 -1.2289,0 -2.2578,0.3527 -3.0868,1.0524 -0.8232,0.6997 -1.3935,1.7698 -1.7051,3.2044l-4.4391 -0.5527c0.1234,-2.0578 0.9995,-3.8041 2.6223,-5.2387 1.6286,-1.4346 3.757,-2.152 6.4029,-2.152 2.7752,0 4.9859,0.7291 6.6322,2.1814 1.6404,1.4522 2.4635,3.1397 2.4635,5.0741 0,1.0642 -0.3057,2.0755 -0.9054,3.028 -0.6056,0.9525 -1.8933,2.2519 -3.8688,3.8923 -1.0231,0.8525 -1.6581,1.5346 -1.905,2.052 -0.2469,0.5174 -0.3587,1.4405 -0.3351,2.7752zm-4.3921 6.5087l0 -4.8389 4.8389 0 0 4.8389 -4.8389 0z"/>
</Button.Content>
</Button>
</Grid>
|
WPF Binding to three level ObservableCollection<T> property
Date : March 29 2020, 07:55 AM
will help you I have 3 level subclasses with ObservableCollection properties of each other. In MainViewModel I created ObservableCollection property which elements of Group class will be in first level in TreeView. In every Group class I created child ObservableCollection property. And in the end in Parameter class I created ObservableCollection for store values. Note: every class based on INotifyPropertyChanged interface. Let's to go the code. , In your ListView ItemSource you have to bind to the Values like so...<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25*" />
<ColumnDefinition Width="75*" />
</Grid.ColumnDefinitions>
<TreeView x:Name="trv" Grid.Column="0" ItemsSource="{Binding Groups}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Parameters}">
<TextBlock Text="{Binding GroupName}" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ParameterName}"></TextBlock>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<ListView Grid.Column="1"
Background="Bisque"
ItemsSource="{Binding SelectedItem.Values, ElementName=trv}">
<ListView.View>
<GridView>
<GridViewColumn Header="Date Time" DisplayMemberBinding="{Binding DateTimeValue}"/>
<GridViewColumn Header="Value" DisplayMemberBinding="{Binding Value}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
|
Angular2 ngModel binding in the third property level gets undefined
Date : March 29 2020, 07:55 AM
|