✍ ComboBox의 BackGround 속성을 변경해도 변경되지 않는 걸 볼 수 있다.
✔ 그래서 비하인드 코드에서 설정을 해줘야 한다.
namespace WpfAttachedProperty
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
cmb.SetBackground(Brushes.Red);
}
}
}
💻 ComboBoxBackgroundExtension
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace WpfAttachedProperty
{
public static class ComboBoxBackgroundExtension
{
public static void SetBackground(this ComboBox combo, Brush brush)
{
var toggleButton = (ToggleButton)combo.Template.FindName("toggleButton", combo);
if (toggleButton != null)
{
var border = (Border)toggleButton.Template.FindName("templateRoot", toggleButton);
if (border != null)
{
border.Background = brush;
}
}
var textBox = (TextBox)combo.Template.FindName("PART_EditableTextBox", combo);
if (textBox != null)
{
var parent = (Border)textBox.Parent;
parent.Background = brush;
}
}
}
}
그러나 이 방법은 MVVM 패턴에 위반 되므로 다른 방법으로 다시 할 예정
💻 ComboBoxBackgroundExtension
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace WpfAttachedProperty
{
public static class ComboBoxBackgroundExtension
{
public static void SetBackground(this ComboBox combo, Brush brush)
{
var toggleButton = (ToggleButton)combo.Template.FindName("toggleButton", combo);
if (toggleButton != null)
{
var border = (Border)toggleButton.Template.FindName("templateRoot", toggleButton);
if (border != null)
{
border.Background = brush;
}
}
var textBox = (TextBox)combo.Template.FindName("PART_EditableTextBox", combo);
if (textBox != null)
{
var parent = (Border)textBox.Parent;
parent.Background = brush;
}
}
}
}
💻 ComboBoxBackgroundManager
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.Media;
namespace WpfAttachedProperty
{
public class ComboBoxBackgroundManager
{
private static Brush? _newBrush = null;
public static Brush GetBackground(DependencyObject obj)
{
return (Brush)obj.GetValue(BackgroundProperty);
}
public static void SetBackground(DependencyObject obj, Brush value)
{
obj.SetValue(BackgroundProperty, value);
}
public static readonly DependencyProperty BackgroundProperty =
DependencyProperty.RegisterAttached("Background", typeof(Brush), typeof(ComboBoxBackgroundManager), new UIPropertyMetadata(Brushes.Transparent, BackgroundChanged));
private static void BackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var cmb = d as ComboBox;
if(cmb == null) return;
if(e.NewValue != e.OldValue)
{
_newBrush = (Brush)e.NewValue;
cmb.Loaded -= Cmb_Loaded;
cmb.Loaded += Cmb_Loaded;
cmb.Unloaded -= Cmb_Unloaded;
cmb.Unloaded += Cmb_Unloaded;
}
}
private static void Cmb_Unloaded(object sender, RoutedEventArgs e)
{
var cmb = (ComboBox)sender;
cmb.Loaded -= Cmb_Loaded;
cmb.Unloaded -= Cmb_Unloaded;
_newBrush = null;
}
private static void Cmb_Loaded(object sender, RoutedEventArgs e)
{
var cmb = (ComboBox)sender;
cmb.SetBackground(_newBrush ?? Brushes.Transparent);
}
}
}
💻 MainWindow.xaml / cs
<Window x:Class="WpfAttachedProperty.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:WpfAttachedProperty"
mc:Ignorable="d"
Title="MainWindow" Height="323" Width="403">
<StackPanel>
<TextBlock Text="ComboBox"/>
<ComboBox x:Name="cmb"
IsEditable="True"
Margin="5 0"
local:ComboBoxBackgroundManager.Background="Blue"/>
</StackPanel>
</Window>
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.Controls.Primitives;
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 WpfAttachedProperty
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
}
}
}
💻
<Window x:Class="WpfAttachedProperty.MainView2"
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:WpfAttachedProperty"
mc:Ignorable="d"
Title="MainView2" Height="450" Width="800">
<Grid>
<ComboBox Grid.Row="0" local:ComboBoxBackgroundManager.Background="Yellow"/>
</Grid>
</Window>
--> 이렇게 새로운 파일은 만들어서 배경색을 변경해도 잘 적용되는 걸 볼 수 있다.
범용적으로 AttachedProperty를 사용할 수 있다.
'C# > WPF' 카테고리의 다른 글
[WPF] Dependency Injection(DI) IoC 를 이용한 Navigation (0) | 2023.08.23 |
---|---|
[WPF] 카카오톡 구현 7 - ComboBox UserControl 만들기 (0) | 2023.08.22 |
[WPF] 카카오톡 구현 5 - PasswordBox WaterMarkText, Validating 만들어보기 (0) | 2023.08.22 |
[WPF] 카카오톡 구현하기 4 - TextBox WaterMarkText, Validating 만들어보기 (0) | 2023.08.21 |
[WPF] 카카오톡 구현 3 - 로그인 화면 만들기(Login Control) (0) | 2023.08.20 |