kyg0711
7์ 14, 2023, 1:52์ค์
1
์๋
ํ์ญ๋๊น.
์ Applcation์๋ 8๊ฐ์ ๊ฐ์ ๋ชจ์์ Toggle button์ด ์กด์ฌํฉ๋๋ค. User Control๋ก ๋ง๋ค์ด์ ์ฌ์ฉ์ค์ด๊ณ , Command์ Command Parameter๋ฅผ Dependency Property๋ก ๋ง๋ค์ด์ ์ฌ์ฉํ๋ ค๊ณ ํฉ๋๋ค.
8๊ฐ์ Toggle button์ ํ๋์ Command๋ฅผ ๋ฐ์ธ๋ฉ ๋ฐ๊ณ Parameter๋ก ํจ์์ ๋์์ ๋๋๋ ค๊ณ ํ๋๋ฐ,
xaml๋จ์์ ๊ธฐ๋ณธ๊ฐ ํ์๊ณผ ์ผ์นํ์ง ์๋๋ค๊ณ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค์. ์ ๊ฐ ๋ญ ์๋ชป ํ์๊น์?
View
<StackPanel Height="72" Width="auto" Margin="0 -10 0 75"
HorizontalAlignment="Center" Orientation="Horizontal">
<togglebtn:outputbtn Margin="20 30 00 0"
DOCommand="{Binding DOControl}"
DOCommandParameter="{Binding DoNums[0]}"/>
<togglebtn:outputbtn Margin="0 30 00 0"
DOCommand="{Binding DOControl}"
DOCommandParameter="{Binding DoNums[1]}"/>
<togglebtn:outputbtn Margin="0 30 00 0"
DOCommand="{Binding DOControl}"
DOCommandParameter="{Binding DoNums[2]}"/>
<togglebtn:outputbtn Margin="0 30 20 0"
DOCommand="{Binding DOControl}"
DOCommandParameter="{Binding DoNums[3]}"/>
</StackPanel>
<StackPanel Height="40" Margin="0 0 0 0" Width="346"
Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Text="DO5" Margin="42 10 40 0" Height="auto"/>
<TextBlock Text="DO6" Margin="15 10 40 0" Height="auto"/>
<TextBlock Text="DO7" Margin="15 10 40 0" Height="auto"/>
<TextBlock Text="DO8" Margin="15 10 40 0" Height="auto"/>
</StackPanel>
<StackPanel Height="72" Margin="0 61 0 0"
HorizontalAlignment="Center" Orientation="Horizontal">
<togglebtn:outputbtn Margin="20 15 00 0"
DOCommand="{Binding DOControl}"
DOCommandParameter="{Binding DoNums[4]}"/>
<togglebtn:outputbtn Margin="0 15 00 0"
DOCommand="{Binding DOControl}"
DOCommandParameter="{Binding DoNums[5]}"/>
<togglebtn:outputbtn Margin="0 15 00 0"
DOCommand="{Binding DOControl}"
DOCommandParameter="{Binding DoNums[6]}"/>
<togglebtn:outputbtn Margin="0 15 20 0"
DOCommand="{Binding DOControl}"
DOCommandParameter="{Binding DoNums[7]}"/>
UserControl.xaml.cs
public ICommand DOCommand
{
get { return (ICommand)GetValue(DOCommandProperty); }
set { SetValue(DOCommandProperty, value);}
}
public static DependencyProperty DOCommandProperty =
DependencyProperty.Register(nameof(DOCommand), typeof(ICommand), typeof(outputbtn), new PropertyMetadata(null, RaiseCommandPropertyCallback));
private static void RaiseCommandPropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
outputbtn command = (outputbtn)d;
ToggleButton btn = command.btnDO;
btn.Command = e.NewValue as ICommand;
}
public int DOCommandParameter
{
get { return (int)GetValue(DOCommandParameterProperty); }
set { SetValue (DOCommandParameterProperty, value); }
}
public static DependencyProperty DOCommandParameterProperty =
DependencyProperty.Register(nameof(DOCommandParameter), typeof(int), typeof(outputbtn), new PropertyMetadata(null, RaiseParameterPropertyCallback));
private static void RaiseParameterPropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
outputbtn patameter = (outputbtn)d;
ToggleButton btn = patameter.btnDO;
btn.CommandParameter = int.Parse((string)e.NewValue);
}
View Model
private List<int> _donums;
public List<int> DoNums { get { return _donums; } }
private RelayCommand<int> _docontrol;
public ICommand DOControl
{
get
{
if (_docontrol == null)
{
_docontrol = new RelayCommand<int>(DoOnOff);
}
return _docontrol;
}
}
private void DoOnOff(int par)
{
MessageBox.Show($"toggle button num : {par}");
}
IOPageVM() //ctor
{
_donums = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, };
}
1๊ฐ์ ์ข์์
dimohy
7์ 14, 2023, 1:56์ค์
2
๊ธฐ๋ณธ๊ฐ์ด null์ธ๋ฐ์ int์ null์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ํ ์ ์์ผ๋ฏ๋ก ํ์
๋ถ์ผ์น์์ ๋ฐ์ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
0 ๋ฑ์ผ๋ก ๋ฐ๊พธ์๋ฉด ๋ฉ๋๋ค.
1๊ฐ์ ์ข์์
kyg0711
7์ 14, 2023, 2:01์ค์
3
๋ค ํด๊ฒฐ๋์ต๋๋ค. ๊ทผ๋ฐ ํ๋ก๊ทธ๋จ์ ์คํํ๋ ํ ๊ธ ๋ฒํผ์ด ํด๋ฆญ์ด ์๋๋ค์. ํน์ ์ด์ ๋ฅผ ์๊ณ ๊ณ์ค๊น์?
dimohy
7์ 14, 2023, 2:06์ค์
4
๊ณต์ ๋ ์ฝ๋๋ก๋ ๋ช
ํํ ์ ์๋ ์์ง๋ง ํ ๊ธ ๋ฒํผ ์ด๋ฒคํธ์ ์ํ ๋์์ Command๋ก ์ ๋ฌํ๋ ์ฝ๋๊ฐ ๋ณด์ด์ง ์๋๋ฐ์, ์๋ง ๊ทธ ๋ฌธ์ ์ผ ๋ฏ ํฉ๋๋ค.
1๊ฐ์ ์ข์์
kyg0711
7์ 14, 2023, 2:26์ค์
5
์ดํด๊ฐ ์ ์๊ฐ๋ค์. xaml์์ Command๋ฅผ ๋ฐ์ธ๋ฉ ํ์ผ๋ ์ด๋ฒคํธ๋ฅผ ๋๊ฒจ ์ค๊ฑฐ ์๋๊ฐ์?
heyjude
7์ 14, 2023, 8:13์ค์
6
์ผ๋ถ ์ฝ๋๋ง ๊ณต์ ํด ์ฃผ์
์ ์ ๋ชจ๋ฅด๊ฒ ์ผ๋, ํน์ ํด๋น View ์ ViewModel ์ฐ๊ฒฐ์ ์ ๋์ด ์์๊น์? DataContext ์ง์ ํ๋ ๋ถ๋ถ์ด ์๋ณด์ฌ์์.
1๊ฐ์ ์ข์์
kyg0711
7์ 14, 2023, 8:18์ค์
7
<Page.DataContext>
<vm:IOPageVM/>
</Page.DataContext>
์์ ๊ฐ์ด ํด๋น View์ ViewModel๊ณผ ์ฐ๊ฒฐ ๋์ด ์์ต๋๋ค. ํ์
์ ์ฌ๋ผ๊ฐ ํ ๊ธ ๋ฒํผ์ธ๋ฐ, ํด๋น ํ์
์ ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ค๋ ๋ฐ์ธ๋ฉ์ด ๋๊ณ ์๋๋ฐ ์์กด ์์ฑ์ผ๋ก ์ ์ ์ปจํธ๋กค ํด๋ณด๋๊ฒ ์ฒ์์ด๋ผ ์ด๋ ต๋ค์.
์๋ง๋ view model ์ฐ๊ฒฐ์ ์ ๋์์ ๊ฑฐ ๊ฐ๊ธดํ๋ฐ
UserControl.xaml.cs ์ด๋ผ๊ณ ํ์ํ์ ์ฝ๋๊ฐ outputbtn ์ ๊ตฌํํ ์ฝ๋์ธ ๊ฑฐ์ฃ ?
๊ทธ๋ฆฌ๊ณ Button ์ ์์๋ฐ์์ ๊ตฌํํ ๊ฑฐ ๋ง์ฃ ?
๊ทธ๋ ๋ค๋ฉด outputbtn ์์ click ์ด๋ฒคํธ๋ฅผ command ๋ก ์ฐ๊ฒฐํ๋ ๋ถ๋ถ๋ ๊ตฌํ๋์ด ์๋์?
๊ธฐ์กด button ์ click ์ด๋ toggle ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋ ์ฌ์ฉํ๋ command ๋ฅผ ํจ๊ป ๊ตฌํํ๊ฑฐ๋ ์ฐ๊ฒฐํด์ฃผ์ด์ผ ์๋ํ๋๋ก ๋์ํ ๊ฑฐ ๊ฐ์์.
๊ทธ ๋ถ๋ถ์ด ๊ตฌํ๋์ด ์์ง ์์ ์ํ์์ ICommand ์์ฑ๋ง usercontrol ์ ์ถ๊ฐ ๊ตฌํํ๋ฉด
์๋ฌด์ผ๋ ์ผ์ด๋์ง ์์ต๋๋คโฆ;;
1๊ฐ์ ์ข์์
heyjude
7์ 14, 2023, 2:44์คํ
9
outputbtn UserControl ์์ ์์กด ์์ฑ์ ์ ๋๋ก ๋ฐ์ธ๋ฉ ํ๋์ง ํ์ธํด ๋ณด์ธ์. ์ฝ๋๊ฐ ๊ณต์ ๋์ด ์์ง ์์ ๋์ ๋๋ฆฌ๊ธฐ๊ฐ ์ข ํ๋ญ๋๋ค. ^^
1๊ฐ์ ์ข์์
kyg0711
7์ 15, 2023, 11:24์ค์
10
<StackPanel>
<StackPanel.Resources>
<Style x:Key="toggleBtn" TargetType="{x:Type ToggleButton}">
<Setter Property="Margin" Value="10,0"/>
<Setter Property="Height" Value="30" />
<Setter Property="Width" Value="60" />
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Content" Value="{DynamicResource Off}"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="{DynamicResource On}"/>
<Setter Property="Background" Value="{DynamicResource ismouseovercolor}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource ismouseovercolor}"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<ToggleButton x:Name="btnDO" Style="{StaticResource toggleBtn}"/>
</StackPanel>
์ ์ฝ๋๊ฐ outputbtn์ ์ฝ๋์
๋๋ค. click ์ด๋ฒคํธ๋ฅผ command์ ์ฐ๊ฒฐํด์ผ ๋๋ค๊ณ ํ์๋๊ฒ ์์ ํ ๊ธ ๋ฒํผ์ Click = "{binding ~~}"์ด๋ฐ ์์ผ๋ก ๋์ผ ํ๋ค๊ณ ๋ง์ํ์๋๊ฑด๊ฐ์?
์, ์ ๋ ์ด์ ์ ์ฒด ์์ค ์ฝ๋๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉด์ ํ์ธํ ๊ฒ ์๋๋ผ
์ ํํ ์ด๊ฑฐ๋คโฆ ๋ผ๊ณ ๋ง์์ ๋ชป ๋๋ฆฌ๊ฒ ๊ณ ๊ทธ๋ฅ ์ ์ํ์ ์ฝ๋ ์์์ ์ถ์ธก์ ํ ๊ฑด๋ฐ์.
UserControl.xaml.cs ์ ์ผ๋ถ๋ง ์ ์ํ์ ๊ฑธ๋ก๋ด์
ToggleButton ์ ์์ํด์ ๋ญ๊ฐ ์ฌ์ ์ ํ ๊ฑด ์๋ ๊ฑฐ ๊ฐ๊ตฐ์.
ํด๋ฆญ์ด ์ ๋๋ค๋ ๊ฒ ์ด๋ค ๋ง์ธ์ง ์ ํํ ์ ๋ชจ๋ฅด๊ฒ ์ด์
ํด๋ฆญ ๋ฒํผ์ด ํ์ฑํ ์ ๋์ด์ ๋๋ฅด๋ ๊ฒ ๋ถ๊ฐ๋ฅํ๋ค๋ ์๊ธฐ์ธ์ง, UI ๋ก ํด๋ฆญ์ ๋๋๋ฐ ์ด๋ฒคํธ ๋ฐ์์ด ์ ๋๋ค๋ ๊ฑด์ง, ์ด๋ฒคํธ๋ ๋ค์ด์ค๋๋ฐ command ํธ์ถ์ด ์ ๋๋ค๋ ๊ฑด์งโฆ
์ ํํ ์๋ฏธ๋ฅผ ๋ชจ๋ฅด๊ฒ ๋ค์. ์ด๊ฒ๋ถํฐ ์ ํํ ์์์ผ ํ ๊ฑฐ ๊ฐ์์.
๊ทธ๋ฅ ์ฌ๊ธฐ์ command ํธ์ถ์ด ์ ๋๋ค๋ ๊ฑธ๋ก ๊ฐ์ ํ์ ๋ ๋๋ ์๋ฌธ์ธ๋ฐ์. UserControl.xaml.cs์์ ๋ณ๋์ ICommand ์์ฑ์ ์ ์ํ์ ์ด์ ๊ฐ ์์๊น์?
๋ฑํ ์ง๊ธ ์ฌ์ฉ๋ฒ์ด๋ ์ฐ๊ฒฐ ๊ตฌ๋ฌธ์ผ๋ก ๋ดค์ ๋์๋ ๊ทธ๋ค์ง ํ์ํ์ง ์์๋ณด์ฌ์์. ์ด์ฐจํผ ์๋ Button ์ ์์ฑ์ผ๋ก ์กด์ฌํ๋ ๊ฑด๋ฐ ๋ฐ๋ก ์ ์ํ๋ ์ด์ ๋ฅผ ๋ชจ๋ฅด๊ฒ ๋ค์. ํน๋ณํ ์ ์ฉ command ๋ฅผ ๋ง๋ค๊ฑฐ๋ ์กฐ์ํ๋ ๊ตฌ๋ฌธ์ ์์ด๋ณด์ด๋ ๋ฐ ๋ง๋์?
ICommand ๊ฐ ๊ตฌํ๋ ๋์ ๊ฐ์ฒด๋ฅผ ์ ํํ๊ณ ์ถ๋ค๋ฉด ๊ทธ๋ฅ DataContext ์ ํ ๊ตฌ๋ฌธ์ Binding ๊ตฌ๋ฌธ์ ์ถ๊ฐํ๋ ๊ฒ ๋ ๋์ ์๋ ์๊ฒ ๋ค์.
์ผ๋จ ๊ทธ๋ ๋ค๊ณ ์น๊ณ , ๋ง์ฝ ์ง๊ธ ์ฝ๋์์ ์ ์์ ์ผ๋ก ๋น๋๋๊ณ Command ์ฐ๊ฒฐ์ด ์ ์์ด๋ผ๊ณ ํ ๋ ๋๋ฒ๊น
์ ํตํด ์ด๋๊น์ง ์ด๋ฒคํธ ํธ์ถ์ด ์งํ๋์๋์ง ํ์ธํด๋ณด์
จ๋์ง์?
์ฌ์ค ๋๋ฒ๊น
๋ฌธ์ ๋ ์ด๋ ๊ฒ ์กฐ๊ฐ๋ ์ฝ๋๋ง ๊ฐ์ง๊ณ ๋ ์ ์ ์๋ ๊ฒ ๋ณ๋ก ์์ด์ ํฐ ๋์์ ๋ชป ๋๋ฆด ๊ฑฐ ๊ฐ๊ณ , ๊ทธ๋ฅ ์ถ์ธก์ฑ ๋ฐ์ธ ๋ช ๊ฐ์ง๋ค๋ง ์๋ค๊ฐ๋ค ํ ์๋ฐ์ ์๊ฑฐ๋ ์
๊ทธ ๋ค์๋ถํฐ๋ ์ค์ค๋ก ๋๋ฒ๊น
ํด์ ์ฐพ์๋ด๋ ๋ฐฉ๋ฒ๋ฐ์ ์๊ธดํด์.
๊ฐ์ฅ ์ข์ ๊ฑด ์ฌํ๊ฐ๋ฅํ ์์ค์ฝ๋๋ฅผ ๊ฐ์ด ์ฌ๋ ค์ฃผ์๋ ๊ฒ ๊ฐ์ฅ ์ข์ ๊ฑฐ ๊ฐ์๋ฐ์. ์ง๊ธ์ผ๋ก์ฌ ์์ ๋๊ฐ ์ต์ ์ผ ๊ฑฐ ๊ฐ์ด๋ค.
3๊ฐ์ ์ข์์
dimohy
7์ 16, 2023, 9:47์ค์
12
์ด ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ์๊ฒ ์ด์? UserControl ์์ ToggleButton์ Command์ ๋๊ธฐ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
| MainWindow.xaml
<Window
x:Class="WpfApp41.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:local="clr-namespace:WpfApp41"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<StackPanel Orientation="Vertical">
<local:CustomToggleButton
Command="{Binding ToggleCommand}"
CommandParameter="1"
Text="1" />
<local:CustomToggleButton
Command="{Binding ToggleCommand}"
CommandParameter="2"
Text="2" />
<local:CustomToggleButton
Command="{Binding ToggleCommand}"
CommandParameter="3"
Text="3" />
<local:CustomToggleButton
Command="{Binding ToggleCommand}"
CommandParameter="4"
Text="4" />
<local:CustomToggleButton
Command="{Binding ToggleCommand}"
CommandParameter="5"
Text="5" />
</StackPanel>
</Grid>
</Window>
| MainWindow.xaml.cs
โป Command๋ฅผ ๊ฐ๋จํ๊ฒ ๊ตฌํํ๊ธฐ ์ํด ๋ณ๋์ ViewModel์ ์๋ตํ๊ณ xaml.cs์ Community Toolkit MVVM์ ์ฌ์ฉํ์ฌ Command๋ฅผ ๊ตฌํ ํ์์ต๋๋ค.
using CommunityToolkit.Mvvm.Input;
using System.Windows;
namespace WpfApp41
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
[RelayCommand]
void OnToggle(int num)
{
MessageBox.Show(num.ToString());
}
}
}
| CustomToggleButton.xaml
<UserControl
x:Class="WpfApp41.CustomToggleButton"
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:local="clr-namespace:WpfApp41"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="this"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Grid>
<ToggleButton
Command="{Binding Command, ElementName=this}"
CommandParameter="{Binding CommandParameter, ElementName=this}"
Content="{Binding Text, ElementName=this}" />
</Grid>
</UserControl>
| CustomToggleButton.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApp41;
/// <summary>
/// CustomToggleButton.xaml์ ๋ํ ์ํธ ์์ฉ ๋
ผ๋ฆฌ
/// </summary>
public partial class CustomToggleButton : UserControl
{
public readonly static DependencyProperty CommandProperty = DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(CustomToggleButton), new PropertyMetadata(null));
public readonly static DependencyProperty CommandParameterProperty = DependencyProperty.Register(nameof(CommandParameter), typeof(int), typeof(CustomToggleButton), new PropertyMetadata(0));
public readonly static DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(CustomToggleButton), new PropertyMetadata(string.Empty));
public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
public int CommandParameter
{
get => (int)GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public CustomToggleButton()
{
InitializeComponent();
}
}
| ์คํ
4๊ฐ์ ์ข์์
์โฆ ๊ทธ๋ฆฌ๊ตฌ ๋ง๋ถ์ด์๋ฉด
์ด๋ฐ ์ํฉ์์๋ UserControl ๋ณด๋ค๋ CustomControl ์ ์ฌ์ฉํ๋ ๊ฒ ๋ ๋์๋ณด์๋ค.
์ฌ์ค UserControl ์ด๋ CustomControl ์ด๋ view ๋ฅผ ๋ง๋ ๋ค๋ ์ฐจ์์์๋ ํฐ ์ฐจ์ด๊ฐ ์์ ์๋ ์๋๋ฐ
์ ๋ ๊ฐ์ธ์ ์ผ๋ก
Layout ์ ๋ง๋๋ view โ UserControl
componet ๋ฅผ ๋ง๋๋ view โ CustomControl
์ด๋ ๊ฒ ์ ์๋ฅผ ํ๊ณ ์ฌ์ฉํ๋ ํธ์ด์์.
๊ฐ๋ณ control ์์ค์ component ๋ฅผ ๋ง๋ค์ด ๋
๋ฆฝ์ ์ผ๋ก ์ฌ์ฌ์ฉํด์ผํ๋ ์ํฉ์ด๋ผ๋ฉด
CustomControl ๋ก ์ ์ํด ๊ตฌํํ๋ ๊ฒ ํจ์ฌ ๋ ๊ฐ๋จํ๊ณ ์ง๊ด์ ์ผ ์ ์๊ณ , ํ
์คํธํ๊ธฐ ์ฉ์ดํด์ ธ์.
๋ ๋
๋ฆฝ์ ์ผ๋ก ๊ตฌํํ๊ธฐ ๋๋ฌธ์ DataContext ์ ์ ๊ฒฝ์ฐ์ง ์๊ณ ์์ฑํ ์ ์๊ตฌ์. ใ
ใ
ใ
!
UserControl ์ component ๋ฅผ ๋ค ๋๋ ค๋ฐ๊ณ ๋ง๋ค์ด๋ ํฐ ์๊ด์ ์๋๋ฐ
component ๋ฅผ ํฌํจํ๋ UserControl ์ ๊ฒฝ์ฐ DataContext ์ ์์กด์ ์ธ ๊ตฌํ์ด ๋ค์ด๊ฐ ๊ฐ๋ฅ์ฑ์ด ์๊ณ
๋๋ฒ๊น
ํ ๋ ๋
๋ฆฝ์ ์ผ๋ก ๋ฐ๋ผ๊ธฐ ์ด๋ ค์ด ์ํฉ์ด ์ฐ์ถ๋ ์๋ ์์ด์.(์ง๊ธ์ฒ๋ผ๋ง์ด์ฃ .)
๋ง์ฝ toggleBtn ์ CustomControl ๋ก ๊ตฌํํ๋ค๋ฉด
command ์ฐ๊ฒฐ์ ๋๋ฒ๊น
ํ๊ณ ์ถ์ ๋ ์์ ๋ณ๋์ ํ๋ก์ ํธ์ ๋ฏ์ด๋ค๊ฐ ๋ฃ๊ณ
๊ฑฐ๊ธฐ์ ๊ฐ๋จํ View ์ ์ฎ์ด์ ํ
์คํธํด๋ณผ ์ ์์ฃ .(๋ฌธ์ ๋ฅผ ๋จ์ํ ํด์ ์ฐพ๊ธฐ ์ํด์!)
๊ทธ๋ฐ๋ฐ ์ด๋ ๊ฒ UserControl ๋ด๋ถ์ ์ ์ํด ๋ค๋ฅธ view ์ ์์ธ ์ํ๋ก ๊ตฌํํ๋ฉด
์์์ ๋งํ ๋ฌธ์ ๋ฅผ ๋จ์ํํ๋ ๋ฐฉ์์ ๋๋ฒ๊น
์ด ์ด๋ ค์ ์ง๋๋ค.
์โฆ ๋คโฆ ๊ทธ๋ฅ ๊ทธ๋ ๋ค๊ตฌ์โฆ =ใ
=
2๊ฐ์ ์ข์์
kyg0711
7์ 17, 2023, 1:34์ค์
14
๋ค. Toggle Button์ ์์ ๋ฐ์์ ์ฌ์ ์ ํ์ง๋ ์์์ต๋๋ค. 8๊ฐ์ Toggle Button์ด ๋ชจ๋ ๊ฐ์ ์ธํ๊ณผ ๊ธฐ๋ฅ์ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ ํ ๊ฐ์ User Control๋ก ์ ์ํ์ฌ ์ฌ์ฉํ๋ ค๊ณ ํฉ๋๋ค.
ํ๋ก๊ทธ๋จ์ ์คํ ์, ํด๋ฆญ์ด ๋๋๊ฑด์ง๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง ํ ๊ธ ๋ฒํผ์ด ๋๋ฆฌ์ง ์์ต๋๋ค. ์๋๋ ๋๋ฆฌ๊ฒ ๋ ๊ฒฝ์ฐ, ํ ๊ธ ๋ฒํผ์ ์ด๋ฏธ์ง๊ฐ ๋ณํด์ผ ํ๋๋ฐ ๋ณํ์ง ์์ต๋๋ค.
๋ณ๋์ ICommand ์์ฑ์ ์ ์ํ ์ด์ ๋ ๊ทธ๋ ๊ฒ ํด์ผ๋ง ํ ๊ธ ๋ฒํผ์ Check๋ UnCheck ๊ฐ์ ํด๋ฆญ ์ ๋ฐ์ํ๋ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ ์ ์์๊ฑฐ๋ผ๊ณ ์๊ฐ ํ๊ธฐ ๋๋ฌธ์
๋๋ค. User Control์ด ํ์ํ ๋ถ๋ถ์์ ํธ์ถ ํ ์ด๋ฒคํธ๋ฅผ ์ ์ํ ๋ Dependency Property๋ก ์ ์ธํ ์์ฑ๋ค๋ง ๋ณด์ฌ์ ๊ทธ๋ ๊ฒ ์๊ฐํ์ต๋๋ค.
ํน๋ณํ ์ ์ฉ command๋ฅผ ๋ง๋ค์ง๋ ์์ต๋๋ค. ๊ทธ์ User Control์์ ๋ฐ์ํ ์ด๋ฒคํธ๋ฅผ View Model ๋จ์์ ์ฒ๋ฆฌํ๊ณ ์ถ์ ๋ฟ์
๋๋ค.
1๋ฒ์ ์ด์ ๋ก ๋ฐ์ธ๋ฉ ์ค๋ฅ์ธ์ง, ํด๋ฆญ ์ ์ด๋ฒคํธ๊ฐ ์ด๋๊น์ง ํ๋์ง ํ์ธํ์ง ๋ชปํ๊ณ ์์ต๋๋คโฆ
์ฌํ ๊ฐ๋ฅํ ์์ค ์ฝ๋๋ผ๊ณ ํ์๋๊ฒ ์ด๋ ๋ถ๋ถ์ ๋ง์ํ์๋์ง ์ ํํ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. User Control๊ณผ ๋นํ์ธ๋ ์ฝ๋, View์ View Model์ ์์ค ์ฝ๋๋ฅผ ๋ชจ๋ ์ฌ๋ ธ๋๋ฐ ๋ค๋ฅธ ๋ถ๋ถ์ด ๋ ํ์ํ๊ฐ์?
ํ์ํ ๋ถ๋ถ์ ๋ง์ ์ฃผ์๋ฉด ๋น ๋ฅธ ์๊ฐ ๋ด์ ์ฌ๋ ค๋ณด๊ฒ ์ต๋๋ค. ์ง๋ฌธ์ด ์ฒ์์ด๋ค ๋ณด๋ ๋ฏธ์ํ๋ค์. ๋ต๋ณ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
1๊ฐ์ ์ข์์
kyg0711
7์ 17, 2023, 1:36์ค์
15
CustomControl์ ์กด์ฌ๋ฅผ UserControl ๋ณด๋ค ๋ฆ๊ฒ ์์๋ฒ๋ ธ๋ค์โฆ ๋ค์์๋ ๋ง์ ํด์ฃผ์ ๋ด์ฉ ๋ฐ์ํด์ ๊ฐ๋ฐํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. โฆ ๋ง์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
2๊ฐ์ ์ข์์
command ๊ฐ ๋์ํ๋ ์ง์ ๋ํ ๊ฒ๋ง ํ์ ํด์์ค.
์ฐ์ ์ด๊ฒ๋ถํฐ ํ์ธํด์ผํ ๊ฑฐ ๊ฐ์์. ๋ค๋ฅธ view ์ ๋งํ์ ์์ mouse down ์ด ์ ๋ฌ ์ ๋์ ์๋ ์์ด์. ์ ์์ ์ผ๋ก click ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋์ง ํ์ธ๋ถํฐ ํด์ผํ ๊ฑฐ ๊ฐ์ต๋๋ค.
ToggleButton ์ด Checked / Unchecked ์ด๋ฒคํธ๋ฅผ ๋ฐ๋ก ์ง์ํ๊ธดํ๋๋ฐ view ์์ ์ง์ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ๋ถ์ฌ์ ์ฌ์ฉํ๋ ์ํฉ์ด ์๋๋ผ๋ฉด ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ฃผ๋ก ์ฌ์ฉํฉ๋๋ค.
์ด๋ฏธ command ๋ฅผ ๋ถ์ฌ์ ๊ตฌํํ๋ค๋ ๊ฑด toggle ์ํ๊ฐ ๋ณํ๋ ๊ฒ์ ๋ฐ์์ ViewModel ์์ ๋ญ๊ฐ ์ฒ๋ฆฌํ๊ณ ์ถ๋ค๋ ๊ฑธ๋ก ๋ณด์ด๋๋ฐ์.
๊ทธ๋ผ ๋จ์ํ click ์ด๋ฒคํธ์ ๋์ํ๋ Command ์์ฑ์ ์ฐ๊ฒฐํด ์ฒ๋ฆฌํ๊ธฐ๋ณด๋ค๋ IsChecked ์์ฑ์ binding ํด ViewModel ์์ ์ง์ ๋ฐ์์ ์ฌ์ฉํ๋ ๊ฒ ๋ ๋์ ์ ํ ๊ฐ์์.(์ ๋ผ๋ฉด ๊ทธ๋ ๊ฒ ํ๊ฒ ์จโฆ)
์ด๋ฒคํธ๋ฅผ ํ์ธํ๋ ๋ฐฉ๋ฒ์ VS์ visual tree ๋โฆ ์ ๋ snoop ์ ์ฉํฉ๋๋คโฆ(์๋ ์ฌ๋โฆ ;ใ
;)
์ฌํ๊ฐ๋ฅํ ์์ค ์ฝ๋๋ ๋ง ๊ทธ๋๋ก vs ๋ก ์ด์์ ๋ ๋ก๋ฉ ๊ฐ๋ฅํ ์์ค ํน์ ์ ์ด๋ @dimohy ์ ์์ ์ ๋ ์์ค์ ์๊ธฐํฉ๋๋ท ใ
ใ
ใ
/
2๊ฐ์ ์ข์์
Tokhi
2์ 5, 2024, 1:43์ค์
17
ํ๋ฉด๋ง๋ค ๋์ผํ ๊ธฐ๋ฅ์ ํ๋ control์ด ํ์ํด์ ์ ์ ์ปจํธ๋กค๋ก ๋ง๋ค์๋๋ฐ ๊ฐ ํ๋ฉดํ๋ค ์ฒ๋ฆฌ๋ฅผ ์ด์ฐํ๋ ๊ณ ๋ฏผํ๋๋ฐ,โฆ ๊ฐ์ฌํฉ๋๋ค~!!
1๊ฐ์ ์ข์์