0

I have a property in my model class as follows which is an enum.

public class VmOutputCarouselBarCodeServiceControlData : BindableBaseThreadSafe
{
 private SensorBufferState _lockStatus;
 public SensorBufferState LockStatus
        {
            get => _lockStatus;
            set => Set(ref _lockStatus, value);
        }
}

In my VM which is the DataContext of the whole view, I am setting the value as follows,

public class VM
{
public VmOutputCarouselBarCodeServiceControlData VmOutputControlData { get; set; }
public VM()
{
VmOutputControlData=new VmOutputCarouselBarCodeServiceControlData();
VmOutputControlData.LockStatus=SensorBufferState.Active;
}
}


    public enum SensorBufferState
    {
        Default,
        Active,
        InActive,
        Error
    }

In my xaml I have a datatemplate inside a datatemplate as follows:

<DataTemplate x:Key="OutputTemplate">
<Grid>
 <ContentControl Grid.Row="0" 
                 Grid.Column="1" 
                 DataContext="{Binding VmOutputControlData.LockStatus}" 
                 ContentTemplate="{StaticResource ErrorContentControl}"/>
</Grid>
</DataTemplate>

 <DataTemplate x:Key="ErrorContentControl">
                <Grid>
                    <Rectangle x:Name="SensorStatusRectangle"
                               Fill="{Binding Path=.,Mode=OneWay,Converter={StaticResource SensorBufferToColorConverter}}"
                               Height="30"
                               Width="60"/>
                </Grid>
            </DataTemplate>

In the xaml.cs

The data context is set to VM for the entire view

Problem is when I keep a breakpoint inside the converter class it never hits. But while its running when I remove the . from binding Path, the breakpoint comes to the converter. The DataContext of the ContentControl is showing as Active which is one of the enum states. Yet the binding is not working. Please help.

nikhil
  • 1,578
  • 3
  • 23
  • 52

1 Answers1

1

The DataTemplate is actually not applying. It can't.

The DataTemplate or in general the ContentControl.ContentTemplate willl apply to the ContentControl.Content property and not to the ContentControl.DataContext.
This means the DataContext of the DataTemplate is not the DataContext of the ContentControl, but the Content or the value of the ContentControl.Content property.

To make the templating work, you must set the Content instead of the DataContext:

<ContentControl Content="{Binding VmOutputControlData.LockStatus}" 
                ContentTemplate="{StaticResource ErrorContentControl}"/>

Now that the ContentControl has content, the DataTemplate will be applied and the binding will resolve and invoke the converter.

<DataTemplate DataType={x:Type SensorBufferState}">
  <Grid>
    <Rectangle x:Name="SensorStatusRectangle"
               Fill="{Binding Path=., Mode=OneWay, Converter={StaticResource SensorBufferToColorConverter}}" />
  </Grid>
</DataTemplate>
BionicCode
  • 1
  • 4
  • 28
  • 44
  • Thank you, I made the change but even with that only if the Binding Path changes when the app is running, then its hitting the breakpoint. – nikhil Apr 15 '20 at 16:26
  • Thank you I have tried it. Its a stupid Uwp app by the way. I had to change the Binding Path=., Converter=... to x:Bind Converter=... and it works. I think its uwp Binding. – nikhil Apr 15 '20 at 19:55
  • Alright. I see. You posted this question tagged _WPF_. That's why I never considered `x:Bind`. So you used the wrong tag :) Both frameworks share similarities - on the surface. Almost everything is implemented from scratch to adopt to the new runtime environment. The markup extension system (like `Binding`) are no exclusion. They took the chance to improve some fundamental concepts. `x:Bind` is one example. UWP looks and behaves a lot like WPF, but when it comes to details they are really different. How do you like UWP, do you use it professionally? Many companies are reluctant to introduce it – BionicCode Apr 15 '20 at 21:16
  • Yes unfortunately we are stuck with that uwp. Although there are a lot similarities It dosen't have a lot of key features that wpf has like relative source which is really a pain. It dosen't have many small things like the layout transform, Multibinding, MultiDataTriggers etc etc. Even for DockPanel and uniformgrid you have to rely on xaml controls library, add it via nuget. And yea I added the wpf tag because the community for uwp is so small and also for this question I guess it would be the same in wpf. I work for a company called Beckman Coulter. Although a lot of teams use wpf we use uwp. – nikhil Apr 15 '20 at 22:11
  • Yes, that was my observation too. UWP is not finished yet. I like the sandbox like environment. Applications now have more restrictions to prevent the careless developer from violating user privacy. You now can bind to methods. The UI looks very nice with the lighting effects and the and Segoe MDL2 icon set. UWP offers more integration into the Windows 10 environment e.g. native support of dark/light theme and accent colors or theming in general. Reactive UI to target multiple devices. – BionicCode Apr 16 '20 at 00:12
  • Things that are more complicated to implement with WPF, which only targets the classic desktop. But like you said, when scenarios get more complex, you realize that some tools or infrastructure is missing. WPF is by far more mature - it's older. I don't know the roadmap, but I really hope they will continue to enhance the UWP libraries and framework features. I mean you can build business applications, but implementation doesn't feel smooth at times. Good luck with your development. When UWP becomes the new standard you will be a real specialist. – BionicCode Apr 16 '20 at 00:13
  • I don't know why enen though there are frameworks like electron.js people use WPF. But its good to see WPF being used lol. It has a lot of potential. – nikhil Apr 16 '20 at 14:47
  • Haha, you got me. I am an absolute enemy of building business applications using JavaScript. At least plain JavaScript. Electron realizes browser based applications. I am not the biggest fan of using browser applications on a desktop machine. Doesn't feel good. E.g. I like working multiscreen. Browser makes splitting screens very difficult. I think desktop applications have more features especially when we need to go low level. I have had and will have many many discussions about Browser vs desktop application. So I'm quite aware that this is basically a matter of preferences. – BionicCode Apr 16 '20 at 15:28
  • Hi can you answer this question if you get a chance. https://stackoverflow.com/questions/61373596/datatemplate-selector-after-datacontext-set – nikhil Apr 22 '20 at 19:33