question

EduardoGomez-1870 avatar image
0 Votes"
EduardoGomez-1870 asked EduardoGomez-1870 edited

Binding problems

hello

I am making a simple application on wpf, but the textbox doesn't want to bind to my VM

     <Window.DataContext>
         <vm:MainWindowVM />
     </Window.DataContext>
    
     <Grid Margin="5">
         <Grid.RowDefinitions>
             <RowDefinition Height="Auto" />
             <RowDefinition Height="*" />
         </Grid.RowDefinitions>
    
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="Auto" />
             <ColumnDefinition Width="*" />
         </Grid.ColumnDefinitions>
    
         <Label
             HorizontalAlignment="Center"
             VerticalAlignment="Center"
             Content="How many do you want to create: " />
    
         <TextBox Grid.Column="1" Text="{Binding Num}" />
    
         <Button
             Grid.Column="1"
             HorizontalAlignment="Right"
             VerticalAlignment="Center"
             Style="{StaticResource MenuItem}">
    
             <Button
                 Margin="0,0,10,0"
                 VerticalAlignment="Center"
                 Command="{Binding CreateNewTextBoxCommand}"
                 Content="Create"
                 FontStyle="Oblique"
                 Foreground="White"
                 Style="{StaticResource MenuItem}" />
         </Button>
     </Grid>

 public string Num { get; set; }
windows-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered PeterFleischer-3316 edited

HI,
your code work without problems. Please, show your ViewModel. Try following demo:

XAML:

 <Window x:Class="WpfApp1.Window006"
         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:WpfApp1"
         xmlns:vm="clr-namespace:WpfApp006"
         mc:Ignorable="d"
         Title="Window006" Height="450" Width="800">
   <Window.Resources>
     <Style x:Key="MenuItem" TargetType="{x:Type Button}">
       <Setter Property="Margin" Value="5"/>
     </Style>
   </Window.Resources>
   <Window.DataContext>
     <vm:MainWindowVM />
   </Window.DataContext>
    
   <Grid Margin="5">
     <Grid.RowDefinitions>
       <RowDefinition Height="Auto" />
       <RowDefinition Height="*" />
     </Grid.RowDefinitions>
    
     <Grid.ColumnDefinitions>
       <ColumnDefinition Width="Auto" />
       <ColumnDefinition Width="*" />
     </Grid.ColumnDefinitions>
    
     <Label
              HorizontalAlignment="Center"
              VerticalAlignment="Center"
              Content="How many do you want to create: " />
    
     <TextBox Grid.Column="1" Text="{Binding Num}" />
    
     <Button
              Grid.Column="1"
              HorizontalAlignment="Right"
              VerticalAlignment="Center"
              Style="{StaticResource MenuItem}">
    
       <Button
                  Margin="0,0,10,0"
                  VerticalAlignment="Center"
                  Command="{Binding CreateNewTextBoxCommand}"
                  Content="Create"
                  FontStyle="Oblique"
                  Foreground="White"
                  Style="{StaticResource MenuItem}" />
     </Button>
   </Grid>
 </Window>

and classes, please, see attached file because forum doesn't insert code.



x.txt (1.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.