WPF Adorner Visibility Data Binding programmatically - wpf

I'm creating a Loading Adorner that has a swirling icon over it. I tried binding the visibility property directly in the XAML but that actually hides everything inside its hierarchy.
I have this in my XAML:
<AdornerDecorator Visibility="{Binding Path=RootGroup.Loading, Converter={StaticResource VisibilityConverter}}">
<TreeView x:Name="groupTreeView" />
</AdornerDecorator>
and this in my constructor
LoadingAdorner adorner = new LoadingAdorner(groupTreeView);
AdornerLayer.GetAdornerLayer(groupTreeView).Add(adorner);
This isn't want I wanted so I tried binding it in the code instead:
LoadingAdorner adorner = new LoadingAdorner(groupTreeView);
Binding bind = new Binding("RootGroup.Loading");
bind.Source = this.DataContext;
bind.Converter = new VisibilityConverter();
adorner.SetBinding(LoadingAdorner.VisibilityProperty, bind);
AdornerLayer.GetAdornerLayer(groupTreeView).Add(adorner);
This will work if the DataContext is not null because it can actually find RootGroup.Loading. But if it is null then the binding has no source to look at.
So I was wondering what does the XAML databinding use as its .Source ? Binding directly in the XAML binds to the correct property, but it doesn't achieve the same result. So I'm just wondering what I should be setting my .Source to So i can bind to RootGroup.Loading ?
Thanks,
Raul

This doesn't directly answer your question, but why are you using an adorner to get the loading animation effect.
Why not just use a border element that is a sibling of your TreeView that is Z-Ordered on top and then do your animation in that.
So you do something like this
<Grid>
<TreeView />
<Border x:Name="myBorder">... </Border>
</Grid>
Then you can do all your binding in XAML without hiding the entire Visual Tree.

Related

wpf pass datatemplate to new window

i need to send a DataTemplate to a new window for printing purposes.
1) I have create a general Window lets name it PrintPreview that holds the followings:
FlowDocument > BlockUiContainer > ContentControl (Responsible to display the DataTemplate that i will send to it)
The problem is that the bindings inside the datatemplate are not working. (not for all cases )
For example:
i have this datatemplate somewhere in my application
<DataTemplate x:Key="MyPrintPreview">
<DockPanel>
<TextBlock Text="{Binding SomeProperty1,RelativeSource={RelativeSource AncestorType=UserControl}}"></TextBlock>
<TextBlock Text="{Binding Source={StaticResource SomeViewModel},Path=SomeProperty2}"></TextBlock>
</DockPanel>
</DataTemplate>
The above DataTemplate is working very well and show both properties in my current View (UserControl)
but when i send this DataTemplate to the New Window PrintPreview i have the following issues
The 1st TextBlock (SomeProperty1) fails to display the content
The 2nd TextBlock (SomeProperty2) show just fine!
i don't know how to make this work. or if am doing it the wrong way?
You should set or bind the Content property of the ContentControl to an object that contains the properties that the elements in the ContentTemplate try to bind to.
So set the ContentTemplate property of the ContentControl to your DataTemplate and set the Content property to the actual object to bind to. That's how a ContentControl is supposed to be used.
Also note that for your first binding to work, the ContentControl must be a child of a UserControl because you are binding to SomeProperty1 of the parent UserControl. If there is no parent UserControl, the binding will always fail.

Showing UserControl once at the time with DataTemplateSelector

I have a couple specific user controls to Show some Content, e.g. simple like Image, WebControl but also two complex specific custom controls drawing on a canvas.
Now I thought using the DataTemplateSelector to handle the different UserControls. I actully used this http://tech.pro/tutorial/807/wpf-tutorial-how-to-use-a-datatemplateselector as a reference.
I changed the code so the form loads the UserControls dynamically (according to the file extension) in the following collection:
ObservableCollection<string> _pathCollection = new ObservableCollection<string>();
The only difference to the reference is now I want to navigate back and forward to the next control by showing one control only at the time. Which control should I use instead of ListView?
<Grid>
<ListView ScrollViewer.CanContentScroll="False"
ItemsSource="{Binding ElementName=This, Path=PathCollection}"
ItemTemplateSelector="{StaticResource imgStringTemplateSelector}">
</ListView>
</Grid>
How do I need to bind it to the template (equal to ItemTemplateSelector above)? WPF is still very new to me and I am learning.
Use a ContentControl. Bind your current item to the Content-property and the DataTemplateSelector to the ContentTemplateSelector-property.
<ContentControl Content="{Binding Path=CurrentItem, Mode=OneWay}", ContentTemplateSelector="{StaticResource imgStringTemplateSelector}" />
Your CurrentItem should be a DependencyProperty or a INotifyPropertyChanged-property of your DataContext. When you change your CurrentItem, the ContentControl will update the template automatically with help of your TemplateSelector.

Binding inside ContentControl not working

I'm building a graphical designer, based upon an article by Sukram in CodeProject. I'm now trying to extend it so that each item on the canvas binds to a different ViewModel object - i.e. I'm setting the DataContext for each item.
Every item on the designer is actually a ContentControl, into which is placed a different template (based upon which toolbox item was dragged onto the canvas). So I have a template containing a TextBox, and I have a ViewModel object containing a Name property, and I bind the Text property of the TextBox to the Name property of the ViewModel, and ... nothing. I've checked the visual tree with Snoop, and it confirms that the DataContext of the TextBox is the ViewModel object. Yet the TextBox remains empty. And if I modify the (empty) Text in the TextBox, the Name property in the ViewModel does not change. So it looks like the binding is not being applied (or has been removed somehow).
I've found a few posts which talk about the ContentControl messing around with the DataContext and Content properties, but I'm not sure how applicable they all are. The code sets the ContentControl.Content as follows:
newItem = new ContentControl();
ControlTemplate template = toolbox.GetTemplate();
UIElement element = template.LoadContent() as UIElement;
ViewModelItem viewModel = new ViewModelItem() { Name = "Bob" };
newItem.Content = element;
newItem.DataContext = viewModel;
and the XAML for the template is:
<ControlTemplate>
<Border BorderBrush="Black" BorderThickness="1" Width="100">
<TextBox Text={Binding Name}/>
</Border>
</ControlTemplate>
Snoop shows that the TextBox has a DataContext, and if I Delve that DataContext I can see that it has a Name property whose value is "Bob". So why does the TextBox remain empty? Snoop allows me to change that Name property, but the TextBox remains empty.
What am I doing wrong?
A few more details. I've set the VS2010 Debug DataBinding option for the OutputWindow to Verbose, which seems to show that the binding is all being attempted before I set the DataContext. Is it possible that the change to the DataContext is not being recognised?
I've just found this post DataTemplate.LoadContent does not preserve bindings - apparently DataTemplate.LoadContent does not preserve bindings. So it looks like I have to write my own version of LoadContent().
I've realised that the template has come through a XamlWriter, which apparently strips all bindings. This wouldn't be helping.
I've not been able to fix the DataTemplate.LoadContent(), but I realised that I didn't actually need a DataTemplate, since the XamlWriter / XamlReader was already instantiating the UI element that I was after. I found a fix to make the XamlWriter write all the bindings here, and after that it all works.
Thanks for your help.
Maybe you need to tell the binding in the ControlTemplate to look at the TemplatedParent, as is mentioned in this thread?
<TextBox Text="{Binding Path=Name, RelativeSource={RelativeSource TemplatedParent}}"/>
Either that, or try to use a DataTemplate instead.
I can't test this at the moment, so I might just be guessing here.
I would use a DataTemplate, as bde suggests.
You are trying to put some UI on your own data (ViewModel), and this is what Data-Templates are meant for (ControlTemplate is usually what you use if you want to change how e.g. a Button looks).
Change your code to use ContentControl.ContentTemplate with a DataTemplate:
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1" Width="100">
<TextBox Text={Binding Name}/>
</Border>
</DataTemplate>
Code-behind:
newItem = new ContentControl();
//NOTE: .GetTemplate() needs to return a DataTemplate, and not a ControlTemplate:
newItem.ContentTemplate = toolbox.GetTemplate();
ViewModelItem viewModel = new ViewModelItem() { Name = "Bob" };
newItem.Content = viewModel;
newItem.DataContext = viewModel;

Setting x:Name on the UserControl tag inside a UserControl sometimes crashes the application

I've created a UserControl named Marked. The code for the control is :
<UserControl .... x:Name="marker">
<StackPanel Orientation="Vertical" x:Name="LayoutRoot">
<Image Source="{Binding Path=MarkSource, ElementName = marker}" Visiblity="{Binding Path=IsMarked}"/>
</StackPanel>
</UserControl>
In the code behind I've set two dependency properties: MarkSource and IsMarked.
When I use the control I do something like this:
<my:Marker MarkSource="mark.jpg" IsMarked = {Binding Path=Person.IsActive}/>
The problem is:
1. The custom control works.
2. Sometimes when I'm starting the application I receive the following error: Xaml tree error the name marker already exits.
when I restart the application it works correctly.
I've tried removing the x: from the x:Name="marker" to Name="marker" but the binding on the image doesn't work.
I've tried setting up binding in the code behind, it also doesn't work.
What is the problem with the x:Name?
Try to give x:Name to your Image control.
Remove x:Name from your UserControl.
In code behind set DataContext of your image to this.
Remove ElementName = marker in your binding.

Binding Setting Property but UI not updating. Can I debug within referenced project/control?

I have a custom control with bindings like below
<DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
<me:MarkdownEditor
Options="{Binding Path=Options, RelativeSource={RelativeSource AncestorType=Window}}" />
</DataTemplate>
I find that binding (Window1.Options) is being set (after stepping through code in debug mode), the markdown editor options (supposed to set Fonts, Colors etc) does not get set, or at least the UI does not update. I want to bug whats happening with in the MarkdownEditor.xaml.cs but thats another (referenced) project. How can I verify that the MarkdownEditor.Options is being set at least?
I have actually tested that the MarkdownEditor side is working by the below
<Window ...>
<Grid>
<Button Content="Options" Click="Button_Click" Grid.Row="0" />
<me:MarkdownEditor Options="{Binding Options, RelativeSource={RelativeSource AncestorType=Window}}" Grid.Row="1" />
</Grid>
</Window>
So the difference is the latter is a MarkdownEditor just in a Grid in a Window. The one failing is a MarkdownEditor within a TabControl bound to a ObservableCollection<TabViewModel>
Visual Studio Solution Replicating The Problem
I am not really good at explaining things, so a simple project I made up minus all the unnecessary noise uploaded to media fire so you can take a look at whats wrong
The video showing the problem on Screenr
With just a simple usage, editor in a window/grid.
the binding works ok
Then when used in conjunction with TabControl bound to ObservableCollection<EditorTabViewModel>, the binding works as shown in the 2 TextBoxes updating its values. but the editor does not update
After reading Kent Boogaart's answer to this question I think that the right place to change SetValue to SetCurrentValue isn't in the CLR Property but in the constructor for MarkDownEditor.
public MarkdownEditor()
{
InitializeComponent();
//Options = new MarkdownEditorOptions();
this.SetCurrentValue(OptionsProperty, new MarkdownEditorOptions());
DataContext = this;
}
In fact, this will work just as good without this.SetCurrentValue also since Options will be set through the Binding.
To verify that your Binding has in fact been overwritten by SetValue you can add this code in some event for TabUsage (e.g PreviewMouseRightButtonDown for the FontSize TextBox) and the Binding will start to work again.
private void TextBox_PreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MarkdownEditor.MarkdownEditor editor = VisualTreeHelpers.GetVisualChild<MarkdownEditor.MarkdownEditor>(this);
Binding binding = new Binding();
binding.Path = new PropertyPath("Options");
binding.Source = this;
binding.Mode = BindingMode.TwoWay;
editor.SetBinding(MarkdownEditor.MarkdownEditor.OptionsProperty, binding);
}

Resources