I'm trying to create a login screen but I want to do everything through code.
So generating the full UI through C#.
But I'm working with MVVM model and I have data binding on my username but this doesn't work on the standard PasswordBox so I found a namespace online which makes a workarround, it's called PasswordBoxAssistant from FunctionalFun.UI.
In the xaml I can create a passwordbox with the assistant but I'm making my UI through code and I'm not able to find how to use the PasswordBoxAssistant like that.
So I have this:
And want the same thing but in my C#
But like you see in my code, I don't have access to it.
No idea if the xaml will only be to access it with everything is compile or why I can't use it in my C#.
Anybody that can help me implement this or point me to a direction?
It seems PasswordBoxAssistant is an attached property similar to PassWordHelper here:
https://gist.github.com/alamsal/fcefce4fb2d2d70fb2d91ee12741a35f
You attach such properties as described here:
https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms749011(v=vs.100)#attached-properties-in-code
Hence something like
PasswordBoxAssistant.SetBindPassword(yourPasswordBox, true);
Would add that to a variable yourPasswordBox.
You then need to create a binding on the BoundPassword dependency property of course.
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-create-a-binding-in-code?view=netframeworkdesktop-4.8
The property you are binding as target is the PasswordBoxAssistant dependency property.
Bindings are a bit fiddly in code. I have used the approach rarely.
A rough example:
Binding myBinding = new Binding();
// myBinding.Source = ViewModel; Datacontext is default, you probably don't need source.
myBinding.Path = new PropertyPath("SomeStringPropertyInDatacontext");
myBinding.Mode = BindingMode.TwoWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
yourPasswordBox.SetBinding(PasswordBoxAssistant.BoundPasswordProperty, myBinding);
Related
I'm using Dirkster AvalonDock(v4.60.1) with MVVM pattern in my WPF project.
I would like to change the AnchorableView state into float or hide through my View Model but unfortunately there are not much examples for me to refer.
The way I did was to control the view state in a class called LayoutInitializer, which handling the LayoutUpdateStrategy for my AvalonDock.
Here is my XAML code for Avalon Dock:
<avalonDock:DockingManager.LayoutUpdateStrategy>
<helper:LayoutInitializer/>
</avalonDock:DockingManager.LayoutUpdateStrategy>
With the above code, the XAML will create the LayoutInitializer class on its own, and through this class it is able to control the AvalonDock Elements (eg. LayoutRoot, LayoutAnchorable, Container, etc.)
Below is the code of my LayoutInitializer class to set the AnchorableView state (float or hide):
public void AfterInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableShown)
{
anchorableShown.FloatingHeight = 300;
anchorableShown.FloatingWidth = 400;
anchorableShown.FloatingTop = 150;
anchorableShown.FloatingLeft = 1000;
anchorableShown.CanDockAsTabbedDocument = false;
anchorableShown.CanMove = false;
anchorableShown.CanAutoHide = true;
anchorableShown.Float();
//anchorableShown.Hide();
}
It works fine on its own, however in some cases I will need to change the AnchorableView state to Float/Hide manually through my ViewModel.
I've tried to create another new instance of LayoutInitializer class from my ViewModel, but this new created LayoutInitializer class can not access the AvalonDock Elements, and it will also result in duplicate class for LayoutInitializer.
So, How should I set the AnchorableView state from my ViewModel manually?
Q2.
<avalonDock:DockingManager.LayoutUpdateStrategy>
<helper:LayoutInitializer/>
</avalonDock:DockingManager.LayoutUpdateStrategy>
I can think of another way to try, which is bind a property for LayoutInitializer to the XAML code.
Instead of calling
helper:LayoutInitializer/
I may bind a property of LayoutInitializer in my ViewModel with the XAML code, by this method the ViewModel can share the same object of LayoutInitializer class and my ViewModel can also change the AnchorableView state (float/hide)!
But How can I bind the LayoutInitializer from my ViewModel to the XAML code (avalonDock:DockingManager.LayoutUpdateStrategy)?
#One quick question: Does anyone still using AvalonDock for WPF, or are there other Nuget library for docking the view?
It is quite a complicated issue, sorry if my question confuse you.
But I really need some help from you guys! Thanks in advance!
Solution Here is the way to bind the LayoutInitializer in the ViewModel with the View. With this method, you are able to access the AvalonDock Elements, you can freely change the state of the layout document or layout anchorable or even access to the layout root in the LayoutInitializer class.
My name is Max. I'm currently working on a projekt with caliburn micro.
I'm trying to create a plugin based application in C# with the help of MEF.
I'm using WPF. So I stripped my application to the bare minimum. The bindings with caliburn works fine in the test WPF application. The problem what I have now is that the ShellView (WPF UserControl) needs to live in an ElementHost of a WinForm 3rd party application.
I can't figure it out how to create the correct binding. So that the "magic" of caliburn also works in the winform application. Maybe one of you cracks can help me. I googled and tried this an entire day.
Your help would be very much appreciated.
Greets Max
Link to project:
https://www.dropbox.com/s/y88kgnh0wscy2jr/CaliburnMEF_Example.zip?dl=0
Sample that was provided on the CM GitHub its been there a while..., https://github.com/Caliburn-Micro/Caliburn.Micro/tree/master/samples/Caliburn.Micro.WinFormsInterop/Caliburn.Micro.WinFormsInterop
--edit--
You were close compare this code snippet to your current test project. You were missing a few key items... reference comments
protected override void StartRuntime()
{
base.StartRuntime();
var vm = IoC.Get<ShellViewModel>(); // ok
var view = ViewLocator.LocateForModel(vm, null, null); // needed
//binds the viewmodel to the view & wire up controls...
ViewModelBinder.Bind(vm, view, null); // required!
var activator = vm as IActivate; // required
if (activator != null)
activator.Activate(); // required
_host.Child = view; // since Forms is ViewFirst, by default.
}
Just a few extra steps to get it work with the ElementHost control. As for binding it should work as expected. If you are having issues with binding other controls (3rd party), you might have to create Conventions to support them. That is a very dependent on the controls themselves.
If there isn't already a binding for this - can somebody show me how to add a new binding for this (and register it)?
I've tried locating the UIButton.TouchUpInside binding handler code so I could copy that... without luck.
Thanks
Custom bindings are demonstrated and discussed in full in http://slodge.blogspot.co.uk/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html
Further, the 'built-in' custom bindings are all in https://github.com/slodge/MvvmCross/tree/v3/Cirrious/Cirrious.MvvmCross.Binding.Touch/Target
However, since EditingDidBegin is a standard EventHandler delegate (not a custom EventHandler<TEventArgs>) then there's no need for a custom binding - instead the standard binding should work:
var set = this.CreateBindingSet<FirstView, Core.ViewModels.FirstViewModel>();
set.Bind(textField).For("EditingDidBegin").To(vm => vm.MyEditingBeginCommand);
set.Apply();
I'm using XamlReader successfully to load a xaml file and create a FrameworkElement to work with.
The xaml I'm loading has binding expressions in it such as:
<TextBlock Text="{Binding DataContextTextProperty}" />
If I place the FrameworkElement I get back from XamlReader.Load() into a WPF window, the binding all works fine.
However, in this case I'm using Laurent Bugnion's excellent article on creating PNGs from WPF/XAML. Since the result of XamlReader.Load() is written directly to a PNG via a VisualBrush, it seems the necessary mechanics of WPF to invoke binding expressions are bypassed.
This leads me to believe that the actual bindings aren't really being invoked just by calling XamlReader.Load(), or that they're not working because of something I don't know about to do with there not being a visual tree until you add the FrameworkElement to an existing visual tree or something.
Is there something I can do to ensure these bindings are invoked?
Many thanks in advance.
I FIXED IT!!
Ahem, allow me to explain...
I have no idea how I got to it now, but I found a helpful-sounding article on MSDN regarding Initialization for Objects Not in an Object Tree.
In it I found the following code example:
Button b = new Button();
b.BeginInit();
b.Background = Brushes.Blue;
b.Width = b.Height = 200;
b.EndInit();
b.Measure(paperSize);
b.Arrange(new Rect(paperSize));
b.UpdateLayout();
I looked at the (again, excellent) example from Laurent that I mentioned in the question above, and customised the use of XamlReader as follows:
var element = (FrameworkElement)XamlReader.Load(xamlInput);
element.BeginInit();
element.DataContext = dataContext;
...
element.Measure(renderingSize);
element.Arrange(renderingRectangle);
element.EndInit();
element.UpdateLayout();
I added the BeginInit(), EndInit() and UpdateLayout() (though by process of elimination I believe UpdateLayout() is the key) and now the binding expressions in my dynamically-loaded xaml are working correctly. Hurrah!
I'm missing the boat on something here, kids. This keeps rearing its head and I don't know what's going on with it, so I hope my homeys here can help.
When working in Silverlight, when I create bindings in my c# code, they never hold up when the application is running. The declarative bindings from my xaml seem ok, but I'm doing something wrong when I create my bindings in C#. I'm hoping that there is something blindingly obvious I'm missing. Here's a typical binding that gets crushed:
TextBlock tb = new TextBlock();
Binding b = new Binding("FontSize");
b.Source = this;
tb.SetBinding(TextBlock.FontSizeProperty, b);
I've just tried the exact code you just posted and it worked fine, with some changes. I believe the problem is the element you are using for the SetBinding call is not the textblock you want to bind. It should be:
TextBlock tb = new TextBlock();
Binding b = new Binding("FontSize");
b.Source = this;
tb.SetBinding(TextBlock.FontSizeProperty, b);
Make sure you also have a FontSize public property of type double on "this". If "this" is a user control, I would recommend renaming the property so you don't hide the inherited member.
It looks like as of Silverlight 3.1, at least, this is no longer an issue. I can't reproduce it, at any rate.