1

I want to create a list of TextBox in binding with ObservableCollection<String> in a ViewModel but i have a problem. The binding performs only by source to dialog but it not works by dialog to source

in my ViewModel i have

   private ObservableCollection<String> _ListSashDim;

    public ObservableCollection<String> ListSashDim
    {
        get { return _ListSashDim; }
        set {
            if (_ListSashDim != value)
            {
                _ListSashDim = value;
                this.RaisePropertyChanged("ListSashDim");
            }
        }

    }

the list is initialized as below

  private ObservableCollection<string> createListSashPosition(int numAnte)
    {
        ObservableCollection<string> ls = new ObservableCollection<string>();
        for (int i = 0; i < numAnte; i++)
        {
            ls.Add("X");
        }

        return ls;
    }

in my Xaml i have a DataTemplate

 <!--#region DataTemplate ListSashDim-->
    <DataTemplate x:Key="DataTemplateSashDim">
        <ItemsControl>
            <StackPanel Orientation="Vertical" Width="116" HorizontalAlignment="Center" VerticalAlignment="Center">
                <TextBlock FontSize="10" HorizontalAlignment="Center" Text="Sash Dim"></TextBlock>
                <TextBox x:Name="tbSashDim" Width="90" Text="{Binding Path=.,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ></TextBox>
            </StackPanel>
        </ItemsControl>
    </DataTemplate>
    <!--#endregion-->

and an ItemsControl in a StackPanel

<StackPanel   
                Orientation="Horizontal" 
                HorizontalAlignment="Center" 
                VerticalAlignment="Center" FlowDirection="LeftToRight"
                  Margin="0,10,0,0">

                <ItemsControl ItemsSource="{Binding ListSashDim, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                              HorizontalAlignment="Center" 
                              VerticalAlignment="Center" 
                              Margin="0" ItemTemplate="{DynamicResource DataTemplateSashDim}" Padding="8" >
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"></StackPanel>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </StackPanel>

Thanks so much Manuel

2
  • 2
    Binding Path=.,Mode=TwoWay will not magically replace a string in the source collection. You should have a collection of objects with a string property, e.g. like shown here: stackoverflow.com/a/2872964/1136211. Besides that, your DataTemplateSashDim looks odd. Why is there a StackPanel as single item of an ItemControl? That doesn't make sense. Commented Oct 10, 2016 at 8:43
  • 1
    @Clemens Thanks so much. I have Resolved using a collection of object with a property String property. The StackPanel in a single item of an ItemControl is an error of copy and past. Thanks so much. Manuel Commented Oct 10, 2016 at 9:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.