usercontrol의 값을 부모창에 바인딩하는 방법 문의

안녕하세요. 입력버튼을 usercontrol로 만들어서 부모창에 넣었습니다.

UserControl의 버튼입력값을 부모창의 textBox에 표기를 하고 textBox값을 바인딩해서

값을 저장해야 하는데 도통 방법을 모르겠습니다.

일단 usercontrol에서부터 입력 값을 표기하는거부터가 난관인데 도움 부탁드립니다.

원하는 기능

UserControl.cs

  public partial class CalPanel : UserControl
  {

      private string numberStrP = "0";

      public string NumberStr 
      {
          get { return (string)GetValue(inputButton); } set { SetValue(inputButton, value); MessageBox.Show(NumberStr); }
      
      }

      public static readonly DependencyProperty inputButton =
         DependencyProperty.Register("NumberStr", typeof(string), typeof(CalPanel), new PropertyMetadata("0"));

      public CalPanel()
      {
          InitializeComponent();
      }


      private void button_Click(object sender, RoutedEventArgs e)
      {
         // MessageBox.Show("클릭했어요!");
          numberStrP = numberStrP + (sender as Button).Content.ToString();
          NumberStr = numberStrP;
          
      }
  }

Window.xmal

<Window x:Class="WpfApp4.Views.DefectiveList"
      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:WpfApp4.Views"
      mc:Ignorable="d" 
      Height="800" Width="1332" Title="DefectiveList" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="NoResize" AllowsTransparency="true" Background="Transparent"
       xmlns:calControls ="clr-namespace:WpfApp4.Controls"
    xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2">
    <Window.Resources>
        <Style x:Key="hideColumnHeader" TargetType="GridViewColumnHeader">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
        <Style x:Key="FocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Margin="2" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        
    </Window.Resources>
    <Border CornerRadius="10" BorderThickness="1" BorderBrush="Gray" Background="GhostWhite">
        <Grid Margin="0,0,0,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="0.2*"/>
                <RowDefinition Height="0.6*"/>
                <RowDefinition Height="1.5*"/>
                <RowDefinition Height="0.3*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Background="Transparent" >
               
	<!-- UserControl -->		
            <StackPanel HorizontalAlignment="Left" Height="399" Margin="887,44,0,0" Grid.Row="2" VerticalAlignment="Top" Width="376" Background="DarkSlateGray">
                <calControls:CalPanel Height="399" Width="364"/>
            </StackPanel>

	<!-- 아래 텍스트박스에 UserControl의 입력값을 바인딩하고 싶고 값을 InputQty에 저장하고 싶습니다. -->

	 <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="72" Margin="473,103,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding InputQty}" VerticalAlignment="Top" Width="376" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" FontSize="26" Padding="0,0,10,0">
     	<TextBox.Resources>
         	<Style TargetType="{x:Type Border}">
             <Setter Property="CornerRadius" Value="5,5,5,5"/>
             </Style>

            </TextBox.Resources>

            </TextBox>
           
            <Button x:Name="button" Content="등록" FontSize="45" Foreground="ForestGreen" HorizontalAlignment="Left" Height="159" Margin="887,16,0,0" Grid.Row="1" VerticalAlignment="Top" Width="188" BorderThickness="0" Background="Gold">
                
                <Button.Resources>
                    <Style TargetType="{x:Type Border}">
                        <Setter Property="CornerRadius" Value="5,5,5,5"/>
                    </Style>
                </Button.Resources>
            </Button>
            <Button x:Name="button1" Content="삭제" HorizontalAlignment="Left" Height="159" Margin="1080,16,0,0" Grid.Row="1" VerticalAlignment="Top" Width="183" BorderThickness="0" Background="Orange" FontSize="45" Foreground="White">
                <Button.Resources>
                    <Style TargetType="{x:Type Border}">
                        <Setter Property="CornerRadius" Value="5,5,5,5"/>
                    </Style>
                </Button.Resources>
            </Button>

        </Grid>
    </Border>
</Window>

windowViewModel.cs

 public class DefectiveListViewModel : INotifyPropertyChanged
 {
     private string inputQtyP = "0";
     
      
     public string InputQty 
     {
         get { return inputQtyP; } 
         set { inputQtyP = value; OnPropertyChaged("InputQty"); }
     
     }
     
       
     public event PropertyChangedEventHandler? PropertyChanged;

     protected void OnPropertyChaged(string propertyname)
     {
         if (PropertyChanged != null)
         {
             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
         }

     }
 }
1 Like

Window.xaml에서 CalPanel.NumberStr을 VM의 InputQty에 TwoWay 바인딩하고, TextBox.Text에는 VM의 InputQty를 OneWay 바인딩하면 되지 않을까요?

안녕하세요. CalPanel의 NumbserStr을 InputQty에 바인딩하는 방법이 어찌될까요??

1 Like
<StackPanel HorizontalAlignment="Left" Height="399" Margin="887,44,0,0" Grid.Row="2" VerticalAlignment="Top" Width="376" Background="DarkSlateGray">
    <calControls:CalPanel Height="399" Width="364"
                          NumberStr="{Binding InputQty, Mode=TwoWay}"/> <!-- 여기 -->
</StackPanel>

이리 간단할줄이야 ㅎㅎ;;; 너무 복잡하게 생각을 했네요;; 너무 감사합니다~!!^^

1 Like