logo
down
shadow

WPF binding of property with multiple usercontrols level


WPF binding of property with multiple usercontrols level

Content Index :

WPF binding of property with multiple usercontrols level
Tag : wpf , By : Nick Coats
Date : January 11 2021, 05:14 PM

I wish this helpful for you I would try to avoid handling Visibility too much in a ViewModel. Instead, expose a bool e.g. LoggedIn and bind to it in your View. There you can use a Style/DataTrigger to change Visibilies accordingly.
<Style x:Key="LoginVisibilityStyle" TargetType="StackPanel">
     <Style.Triggers>
          <DataTrigger Binding="{Binding LoggedIn}" Value="false">
              <Setter Property="Visibility" Value="Visible"/>
          </DataTrigger>
          <DataTrigger Binding="{Binding LoggedIn}" Value="true">
              <Setter Property="Visibility" Value="Collapsed"/>
          </DataTrigger>
     </Style.Triggers>
</Style>
="{Binding DataContext.LoggedIn, UpdateSourceTrigger=PropertyChanged, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type views:MainView}}}"

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

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


Tag : chash , By : Jaya
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


Tag : javascript , By : mitry
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Set Button Click handler with a DataTrigger
  • Remove lines between rows in WPF grid
  • How we can set scrollbar on last row of data grid in WPF
  • WPF change ItemsPanel and ItemTemplate in code behind
  • Locating binding errors
  • Databinding an enum to a ComboBox in WPF + MVVM
  • Rendering EMF/WMF images in WPF XAML
  • Using Unit of Work design pattern / NHibernate Sessions in an MVVM WPF
  • Implementing commands in MMVM
  • What is the WPF equivalent for the FlowLayoutPanel?
  • Button ControlTemplate and rounded corners
  • Need help understanding WPF MeasureOverride and ArrangeOverride infinity and 0 sizes
  • Key strokes in wpf window hosted in MFC ActiveX running in Internet Explorer
  • WPF - simple relative path - FindAncestor
  • Possible to InvalidateVisual() on a given region instead of entire WPF control?
  • Xaml parse exception is thrown when i define a duplex contract
  • Horizonatal Scrolling in FlowDocumentScrollViewer
  • Setting properties of auto-generated listboxitem
  • ContentTemplateSelector is only called one time showing always the same datatemplate
  • Why do I get a NullReferenceException when using a style on a ContentPresenter?
  • Using TextOptions.TextFormattingMode with FormattedText
  • Setting a theme in WPF
  • How to make an invisible button to run a command
  • WPF DialogResult declaratively?
  • WPF Using commands to slow to update UI controls
  • DataGridCheckBoxColumn immediate binding
  • HeaderedContentControl ItemsSource
  • Can't override a global WPF style that is set by TargetType on a single specific control
  • wpf cancel backgroundworker on application exits
  • WPF charting/visualization of realtime data
  • bind a WPF datagrid to a datatable
  • Is there a way to do simple calculations in a xaml element binding statement other than using a converter?
  • WPF How to get window reference that is loaded from a page that is into it?
  • WPF Check/Uncheck all checkboxes located in a gridview
  • Selected item in listbox unreadable because of the color
  • dynamic ContextMenu in TreeView vs. MVVM
  • wpf prism (CAL) - Closing Window in Popup Region when the ViewModel knows nothing of the View
  • Free WPF Flow charting Component
  • Some doubts in WPF
  • WPF DataGrid programmatic multiple row selection
  • WPF Best point to update a progress bar from BackgroundWorker
  • WPF Validation & IDataErrorInfo
  • How to return a value with Dispatcher.Invoke?
  • Playing audio files in WPF
  • How much better is the WPF Designer in Visual Studio 2010?
  • XAML C# WPF Best efficient way to do an ordered sequence of animations
  • WPF + Communicate between multiple application instances
  • WPF: control.ItemContainerGenerator.Status is NotStarted. How do I tell it to start now?
  • wpf do animation until background worker finishes its work
  • Exit thread automatically when WPF application closes
  • WPF: How do I set the focus on a datagrid to a specific row?
  • Hello World Surface app doesn't work
  • How to make a tooltip appear immediately in Silverlight?
  • wpf textbox flat border style
  • WPF DataGrid DataGridHyperlinkColumn bound to Uri
  • Resize a WPF window, but maintain proportions?
  • WPF element binding across separate windows
  • How to bind a Listview MaxHeight to the current windows height?
  • WPF - DataGrid Column's ToolTip visibility based on the column's data length
  • WPF ListBox Data Binding Works in Designer but not Runtime
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com