Binding to DataTemplte inner control

Topics: Actions & Coroutines, Conventions
Jul 14, 2011 at 1:55 PM

 

<toolkit:ToggleSwitch Header="Selected by the user" x:Name="UserSelect" Grid.Row="2" IsChecked="{Binding UserSelect.IsChecked, Mode=TwoWay}"  cal:Message.Attach="UserSelCheck()" >                
  <toolkit:ToggleSwitch.ContentTemplate>
   <DataTemplate>
                        <StackPanel Margin="0,-3">
                            <TextBox Width="300"  IsEnabled="{Binding UserSelect.IsChecked, Mode=TwoWay}" />
                        </StackPanel>
   </DataTemplate>
  </toolkit:ToggleSwitch.ContentTemplate>
</toolkit:ToggleSwitch>

 

Hello .Sorry for my poor english .

I have used ToggleSwitch from toolkit .

I have added TextBox into the DataTemplate .

Data binding in ToggleSwitch  control works well .

How i can  bind  data to the TextBox ?

Thanks Constantin.

Coordinator
Jul 14, 2011 at 3:33 PM

You have two options:

1. Use standard databinding.

2. To your stack panel add the following attached property: cal:Bind.Model="{Binding}"

Unfortunately, we cannot automatically apply conventions inside DataTemplates because of limits in the underlying platform. If you want to use conventions in those cases, you need the special property in order to hook the functionality.

Jul 14, 2011 at 7:25 PM

 

 

Many thanks for answer .

 

 

I try to add cal:Bind.Model="{Binding}" 

 

 <toolkit:ToggleSwitch Header="Selected by the user" x:Name="UserSelect" Grid.Row="2" IsChecked="{Binding UserSelect.IsChecked, Mode=TwoWay}"  cal:Message.Attach="UserSelCheck()" >                
                <toolkit:ToggleSwitch.ContentTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,-3" cal:Bind.Model="{Binding}">
                            <TextBox Width="300"  IsEnabled="{Binding UserSelect.IsChecked, Mode=TwoWay}" />
                        </StackPanel>
                    </DataTemplate>
                </toolkit:ToggleSwitch.ContentTemplate>

            </toolkit:ToggleSwitch>
 public class LocationSettingsViewModel:Screen
    {
       public ToggleSwitchModel UserSelect  { get; set; }
    

        public LocationSettingsViewModel(ToggleSwitchModel userSelect)
        {
            UserSelect           = userSelect;            
        }

    
    }
public class ToggleSwitchModel : PropertyChangedBase
    {
        private bool _isChecked;

        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                NotifyOfPropertyChange(() => IsChecked);
            }
        }
       
    }

 

I have received exception .

NullReferenceException

   at Caliburn.Micro.ViewModelBinder.<.cctor>b__9(Object viewModel, DependencyObject view, Object context)   at Caliburn.Micro.Bind.<>c__DisplayClass1.<ModelChanged>b__0(Object , RoutedEventArgs )   at Caliburn.Micro.View.<>c__DisplayClass3.<ExecuteOnLoad>b__0(Object s, RoutedEventArgs e)   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

Can you help with this ?