binding without view model

main
syneffort 1 year ago
parent 05ed26de15
commit ff88a03af3
  1. 2
      MVVMwithWPF/MVVMwithWPF/App.xaml
  2. 24
      MVVMwithWPF/MVVMwithWPF/AttachedProperties/MessageExt.cs
  3. 77
      MVVMwithWPF/MVVMwithWPF/Views/BindingSampleWithoutViewModelWindow.xaml
  4. 27
      MVVMwithWPF/MVVMwithWPF/Views/BindingSampleWithoutViewModelWindow.xaml.cs

@ -2,6 +2,6 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMwithWPF" xmlns:local="clr-namespace:MVVMwithWPF"
StartupUri="/Views/BindingSampleWindow.xaml"> StartupUri="/Views/BindingSampleWithoutViewModelWindow.xaml">
<Application.Resources /> <Application.Resources />
</Application> </Application>

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace MVVMwithWPF.AttachedProperties
{
internal class MessageExt
{
public static string GetMessage(DependencyObject obj)
{
return (string)obj.GetValue(MessageProperty);
}
public static void SetMessage(DependencyObject obj, string value)
{
obj.SetValue(MessageProperty, value);
}
public static readonly DependencyProperty MessageProperty = DependencyProperty.RegisterAttached("Message", typeof(string), typeof(MessageExt), new PropertyMetadata(null));
}
}

@ -0,0 +1,77 @@
<Window x:Class="MVVMwithWPF.Views.BindingSampleWithoutViewModelWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:attachedProperties="clr-namespace:MVVMwithWPF.AttachedProperties"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MVVMwithWPF.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="clr-namespace:MVVMwithWPF.Models"
Title="BindingSampleWithoutViewModelWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.Resources>
<models:Person x:Key="CreateUser"
Name="person0001"
Id="1" Sex="True" />
<Style x:Key="SampleContentStyle" TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock FontSize="20" Foreground="DarkSlateBlue"
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(attachedProperties:MessageExt.Message)}" />
<ContentPresenter Grid.Row="1" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Margin="10">
<TextBlock Text="Edit Current User" />
<TextBlock Text="{Binding Source={StaticResource CreateUser}, Path=Id}" />
<TextBox Text="{Binding Source={StaticResource CreateUser}, Path=Name, UpdateSourceTrigger=PropertyChanged}" />
<CheckBox IsChecked="{Binding Source={StaticResource CreateUser}, Path=Sex, Mode=TwoWay}" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Source={StaticResource CreateUser}, Path=Id}" />
<TextBlock Grid.Column="1" Text="{Binding Source={StaticResource CreateUser}, Path=Name}" />
<TextBlock Grid.Column="2" Text="{Binding Source={StaticResource CreateUser}, Path=Sex}" />
</Grid>
</StackPanel>
<StackPanel Grid.Column="1" Margin="10">
<TextBlock Text="Window Title" />
<TextBlock Text="{Binding Source={x:Static Application.Current}, Path=MainWindow.Title}" />
<TextBlock Text="Shutdown Mode" />
<TextBlock Text="{Binding Source={x:Static Application.Current}, Path=ShutdownMode}" />
</StackPanel>
<StackPanel Grid.Row="1">
<TextBox x:Name="tbxHeader" Text="ContentControl의 헤더를 입력하세요." />
<ContentControl attachedProperties:MessageExt.Message="{Binding ElementName=tbxHeader, Path=Text}" Style="{StaticResource SampleContentStyle}">
<ContentControl.Content>
<StackPanel>
<TextBlock Text="여기는 컨텐츠 컨트롤의 컨텐츠 영역입니다."/>
</StackPanel>
</ContentControl.Content>
</ContentControl>
</StackPanel>
</Grid>
</Window>

@ -0,0 +1,27 @@
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 MVVMwithWPF.Views
{
/// <summary>
/// BindingSampleWithoutViewModelWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class BindingSampleWithoutViewModelWindow : Window
{
public BindingSampleWithoutViewModelWindow()
{
InitializeComponent();
}
}
}
Loading…
Cancel
Save