dependency property

main
syneffort 2 years ago
parent a80ae02806
commit 88bea39b8a
  1. 9
      PacticeSolution/DependencyPropertySample2/App.xaml
  2. 17
      PacticeSolution/DependencyPropertySample2/App.xaml.cs
  3. 10
      PacticeSolution/DependencyPropertySample2/AssemblyInfo.cs
  4. 10
      PacticeSolution/DependencyPropertySample2/DependencyPropertySample2.csproj
  5. 20
      PacticeSolution/DependencyPropertySample2/MainWindow.xaml
  6. 62
      PacticeSolution/DependencyPropertySample2/MainWindow.xaml.cs
  7. 9
      PacticeSolution/ListBoxLinqBindingSample/App.xaml
  8. 17
      PacticeSolution/ListBoxLinqBindingSample/App.xaml.cs
  9. 10
      PacticeSolution/ListBoxLinqBindingSample/AssemblyInfo.cs
  10. 10
      PacticeSolution/ListBoxLinqBindingSample/ListBoxLinqBindingSample.csproj
  11. 61
      PacticeSolution/ListBoxLinqBindingSample/MainWindow.xaml
  12. 90
      PacticeSolution/ListBoxLinqBindingSample/MainWindow.xaml.cs
  13. 31
      PacticeSolution/ListBoxLinqBindingSample/Model/Duty.cs
  14. 38
      PacticeSolution/ListBoxLinqBindingSample/SubWindow/SubWindow.xaml
  15. 43
      PacticeSolution/ListBoxLinqBindingSample/SubWindow/SubWindow.xaml.cs
  16. 23
      PacticeSolution/ListBoxLinqBindingSample/ViewModel/Duties.cs
  17. 14
      PacticeSolution/PacticeSolution.sln

@ -0,0 +1,9 @@
<Application x:Class="DependencyPropertySample2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DependencyPropertySample2"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace DependencyPropertySample2
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

@ -0,0 +1,20 @@
<Window x:Class="DependencyPropertySample2.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:DependencyPropertySample2"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Window.ContextMenu>
<ContextMenu MenuItem.Click="ContextMenu_Click">
<MenuItem Header="YELLOW"/>
<MenuItem Header="GREEN"/>
<MenuItem Header="BLUE"/>
</ContextMenu>
</Window.ContextMenu>
<Grid>
<TextBox x:Name="tbMain" Width="100" Height="20"/>
</Grid>
</Window>

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DependencyPropertySample2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// dependency property
public static readonly DependencyProperty MyProperty = DependencyProperty.Register
("MyColor", typeof(string), typeof(MainWindow), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnMyPropertyChanged)));
public MainWindow()
{
InitializeComponent();
}
public string MyColor
{
get { return (string)GetValue(MyProperty); }
set { SetValue(MyProperty, value); }
}
private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MainWindow win = d as MainWindow;
if (win == null)
return;
SolidColorBrush brush = new BrushConverter().ConvertFromString(e.NewValue.ToString()) as SolidColorBrush;
if (brush == null)
return;
win.Background = brush;
win.Title = (e.OldValue == null) ? "Previous: None" : $"Previous: {e.OldValue.ToString()}";
win.tbMain.Text = e.NewValue.ToString();
}
private void ContextMenu_Click(object sender, RoutedEventArgs e)
{
string str = (e.Source as MenuItem).Header as string;
if (string.IsNullOrEmpty(str))
return;
this.MyColor = str;
}
}
}

@ -0,0 +1,9 @@
<Application x:Class="ListBoxLinqBindingSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListBoxLinqBindingSample"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace ListBoxLinqBindingSample
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

@ -0,0 +1,61 @@
<Window x:Class="ListBoxLinqBindingSample.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:ListBoxLinqBindingSample"
xmlns:viewModel="clr-namespace:ListBoxLinqBindingSample.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="450">
<Window.Resources>
<viewModel:Duties x:Key="duties"/>
<DataTemplate x:Key="myTemplate">
<Border x:Name="boarder">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="Duty name: "/>
<TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Name}"/>
<TextBlock Grid.Column="0" Grid.Row="1" Text="Duty type: "/>
<TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding DutyType}"/>
<Separator Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"/>
</Grid>
</Border>
</DataTemplate>
<LinearGradientBrush x:Key="myGradientBrush" StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="DarkSeaGreen" Offset="0"/>
<GradientStop Color="LawnGreen" Offset="0.5"/>
<GradientStop Color="DarkSeaGreen" Offset="1"/>
</LinearGradientBrush>
<Style TargetType="Button">
<Setter Property="Background" Value="{StaticResource myGradientBrush}"/>
<Setter Property="Width" Value="80"/>
<Setter Property="Margin" Value="5"/>
</Style>
</Window.Resources>
<StackPanel Margin="10">
<Button x:Name="btnAdd" Content="Duty registration" Width="200"
Click="btnAdd_Click"/>
<TextBlock Text="Select duty type"/>
<ListBox x:Name="lstType"
SelectedIndex="0"
SelectionChanged="lstType_SelectionChanged">
<ListBoxItem Content="Inside"/>
<ListBoxItem Content="Outside"/>
</ListBox>
<TextBlock Margin="10 10 0 10" Text="Duty"/>
<ListBox x:Name="lstMain" Width="400" Margin="10" HorizontalContentAlignment="Stretch"
ItemTemplate="{StaticResource myTemplate}"
ItemsSource="{Binding}"
SelectionChanged="lstMain_SelectionChanged"/>
</StackPanel>
</Window>

@ -0,0 +1,90 @@
using ListBoxLinqBindingSample.Model;
using ListBoxLinqBindingSample.SubWindow;
using ListBoxLinqBindingSample.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ListBoxLinqBindingSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static Duties _duties = new Duties();
public delegate void RefreshList(DutyType dutyType);
public event RefreshList RefreshListEvent;
public MainWindow()
{
InitializeComponent();
}
internal static Duties Duties
{
get { return _duties; }
set { _duties = value; }
}
private void RefreshListBox(DutyType dutyType)
{
lstType.SelectedItem = null;
lstType.SelectedIndex = dutyType == DutyType.Inside ? 0 : 1;
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
SubWindow.SubWindow subWindow = new SubWindow.SubWindow();
this.RefreshListEvent += new RefreshList(RefreshListBox);
subWindow.UpdateActor = this.RefreshListEvent;
subWindow.Show();
}
private void lstType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lb = sender as ListBox;
if (lb == null)
return;
if (lb.SelectedItem == null)
return;
ListBoxItem item = lb.SelectedItem as ListBoxItem;
string dutyType = item.Content.ToString();
this.DataContext = from duty in _duties
where duty.DutyType.ToString() == dutyType
select duty;
}
private void lstMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lb = sender as ListBox;
if (lb == null)
return;
if (lb.SelectedItem == null)
return;
Duty duty = lb.SelectedItem as Duty;
if (duty == null)
return;
MessageBox.Show($"{duty.Name} :: {duty.DutyType}", "Selected duty");
}
}
}

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListBoxLinqBindingSample.Model
{
public enum DutyType
{
Inside,
Outside
}
internal class Duty
{
public string Name { get; set; }
public DutyType DutyType { get; set; }
public Duty(string name, DutyType dutyType)
{
this.Name = name;
this.DutyType = dutyType;
}
public Duty()
{
}
}
}

@ -0,0 +1,38 @@
<Window x:Class="ListBoxLinqBindingSample.SubWindow.SubWindow"
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:ListBoxLinqBindingSample.SubWindow"
mc:Ignorable="d"
Title="SubWindow" Height="300" Width="300">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<TextBlock Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center"
FontSize="20" FontWeight="Bold"
Text="Position Registration"/>
<TextBlock Grid.Row="1" Margin="10" VerticalAlignment="Center"
Text="Duty name: "/>
<TextBox x:Name="tbxDutyName" Grid.Column="1" Grid.Row="1" Margin="5" Grid.ColumnSpan="2" VerticalAlignment="Center"/>
<TextBlock Grid.Row="2" Margin="10" VerticalAlignment="Center"
Text="Duty type: "/>
<RadioButton x:Name="rbInside" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center"
Content="Inside"/>
<RadioButton x:Name="rbOutside" Grid.Column="2" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center"
Content="Outside"/>
<Button x:Name="btnSave" Grid.Column="1" Grid.Row="3" Height="22" Width="80" VerticalAlignment="Center" HorizontalAlignment="Center"
Content="Save"
Click="btnSave_Click"/>
</Grid>
</Window>

@ -0,0 +1,43 @@
using ListBoxLinqBindingSample.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ListBoxLinqBindingSample.SubWindow
{
/// <summary>
/// SubWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class SubWindow : Window
{
public Delegate UpdateActor;
public SubWindow()
{
InitializeComponent();
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
if (rbInside.IsChecked == false && rbOutside.IsChecked == false)
{
MessageBox.Show("Please select duty type.");
return;
}
DutyType dutyType = rbInside.IsChecked == true ? DutyType.Inside : DutyType.Outside;
MainWindow.Duties.Add(new Duty(tbxDutyName.Text, dutyType));
UpdateActor.DynamicInvoke(dutyType);
}
}
}

@ -0,0 +1,23 @@
using ListBoxLinqBindingSample.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListBoxLinqBindingSample.ViewModel
{
internal class Duties : ObservableCollection<Duty>
{
public Duties()
{
this.Add(new Duty("SALES", DutyType.Outside));
this.Add(new Duty("LOGISTICS", DutyType.Outside));
this.Add(new Duty("IT", DutyType.Inside));
this.Add(new Duty("MARKETING", DutyType.Inside));
this.Add(new Duty("HR", DutyType.Inside));
this.Add(new Duty("FEILD", DutyType.Outside));
}
}
}

@ -59,7 +59,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListSample", "ListSample\Li
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PropertyTriggerSample", "PropertyTriggerSample\PropertyTriggerSample.csproj", "{480DC5E9-1E18-4176-B964-FD2024C5F1C1}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PropertyTriggerSample", "PropertyTriggerSample\PropertyTriggerSample.csproj", "{480DC5E9-1E18-4176-B964-FD2024C5F1C1}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataTriggerSample", "DataTriggerSample\DataTriggerSample.csproj", "{2184CBCF-A59E-4C6F-9BBF-1DC20F132B50}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataTriggerSample", "DataTriggerSample\DataTriggerSample.csproj", "{2184CBCF-A59E-4C6F-9BBF-1DC20F132B50}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ListBoxLinqBindingSample", "ListBoxLinqBindingSample\ListBoxLinqBindingSample.csproj", "{DE5F5671-AB82-4F9E-879A-6FF1FFC49D26}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyPropertySample2", "DependencyPropertySample2\DependencyPropertySample2.csproj", "{AC4DAD4D-BBAB-4E0B-9BB6-5124592BA365}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -183,6 +187,14 @@ Global
{2184CBCF-A59E-4C6F-9BBF-1DC20F132B50}.Debug|Any CPU.Build.0 = Debug|Any CPU {2184CBCF-A59E-4C6F-9BBF-1DC20F132B50}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2184CBCF-A59E-4C6F-9BBF-1DC20F132B50}.Release|Any CPU.ActiveCfg = Release|Any CPU {2184CBCF-A59E-4C6F-9BBF-1DC20F132B50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2184CBCF-A59E-4C6F-9BBF-1DC20F132B50}.Release|Any CPU.Build.0 = Release|Any CPU {2184CBCF-A59E-4C6F-9BBF-1DC20F132B50}.Release|Any CPU.Build.0 = Release|Any CPU
{DE5F5671-AB82-4F9E-879A-6FF1FFC49D26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE5F5671-AB82-4F9E-879A-6FF1FFC49D26}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE5F5671-AB82-4F9E-879A-6FF1FFC49D26}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE5F5671-AB82-4F9E-879A-6FF1FFC49D26}.Release|Any CPU.Build.0 = Release|Any CPU
{AC4DAD4D-BBAB-4E0B-9BB6-5124592BA365}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC4DAD4D-BBAB-4E0B-9BB6-5124592BA365}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC4DAD4D-BBAB-4E0B-9BB6-5124592BA365}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC4DAD4D-BBAB-4E0B-9BB6-5124592BA365}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

Loading…
Cancel
Save