|
No, I want the VM to call into the UC. The UC is stand alone and should handle all it's own validation so it can be used in multiple places.
So I need to expose a method on it and call it from the parent view's VM.
If it's not broken, fix it until it is
|
|
|
|
|
Well, if you're not going to create a VM for the UC, and you need to return a value from the method, then you're stuck with hooking the method up to the VM somehow.
If you don't want to use the code-behind, then you might be able to get away with binding a delegate. (NB: I haven't tried this.)
UC's code behind:
public ValidationResult Validate() { ... }
public Func<ValidationResult> ValidationMethod
{
get { return Validate; }
}
VM:
public Func<ValidationResult> ValidationMethod { get; set; }
private ValidationResult CallValidation()
{
Func<ValidationResult> fn = ValidationMethod;
if (fn == null) return default(ValidationResult);
return fn();
}
View:
<ns:UserControl
ValidationMethod="{Bind ValidationMethod, Mode=OneWayToSource}"
/>
If the binding doesn't work, then you'll have to hook the method up from the code-behind of the view:
public MyView()
{
InitializeComponent();
DataContextChanged += OnDataContextChanged;
}
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var context = e.NewValue as MyViewViewModel;
if (context != null) context.ValidationMethod = theUserControl.ValidationMethod;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Well, when do you want to call Validate? Generally, I'd say you'd call a Validate method when you leave the control. If that's the case, you don't need to call it manually, just have the control call it by itself.
|
|
|
|
|
What does 'Leave the control' mean??
The user clicks the 'Save' button on the tab's toolbar. This causes the parent VM's Save method to execute. It then has to call into the UC to save its changes.
Save changes calls Validate before saving.
There is no leaving the control. It's just validate before save.
If it's not broken, fix it until it is
|
|
|
|
|
I meant, "leave the control" as in lose focus. If you want to execute on a Save button, that's a different story. There are a few ways to do it:
1) use a composite command and have the control attach to it
2) use messenger and send a message (p.s. this does not require a VM unless your messenger service is tightly coupled to ViewModelBase which it shouldn't be)
3) get a reference to the view in your viewmodel and reference the control directly
4) use a bindable property to trigger the validation (this is probably the cleanest way). I.e. in your save method:
ValidateControl = true;
the control would need to expose a DP and call Validate() when its set to true, then afterwards reset it to false.
Or, if you don't have control over the control, then an attached property would accomplish the same thing.
|
|
|
|
|
I agree - the DP is probably the cleanest, easiest way to do this.
Many thanks
If it's not broken, fix it until it is
|
|
|
|
|
I would create a VM for the UC. The parent VM then instantiates the child VM and has a reference to it so can access it's methods.
So any other control that needs to use the UC can instantiate its VM and Bob's your uncle.
PooperPig - Coming Soon
|
|
|
|
|
Generally, I write controls with code behind. Making a VM for a reusable control kinda ties you into an MVVM framework. I like to re-use my controls across apps.
|
|
|
|
|
I want to send a fax using wpf. using rdlc report can we send a fax or not. Thanks in advance
modified 25-Jul-14 5:47am.
|
|
|
|
|
|
Pete O'Hanlon wrote: Use TAPI.
Don't you need a hmm what is that thing, oh yeah a modem to use that.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
You can Google it using the term "sending fax using C#".
They'll provide you with a code or a tutorial and you can then use it in your software.
Favourite line: Throw me to them wolves and close the gate up. I am afraid of what will happen to them wolves - Eminem
~! Firewall !~
|
|
|
|
|
Hi
I have created a class called DatePickerEx.cs that inherits from DatePicker class. Then I created a UserControl called CustomDatePicker.xaml that references the DatePickerEx.cs class in the xaml code.If I bind the "selecteddate" property in xaml at this point, it works good. So far so good.
Now, I reference the CustomDatePicker.xaml usercontrol inside another UserControl, at this point I have no way to set the "SelectedDate" property on the new UserControl called CustomDatePicker.xaml, can someone explain me how can I expose the DatePickerEx.cs->DatePicker.cs SelectedDate property via the new userControl CustomDatePicker.xaml to any other consumers of CustomDatePicker.xaml.
I need to use some kind of Dependency Property to achive this?
Your help will be highly appreciated.
Thank you.
happy coding!
|
|
|
|
|
If you did public class DatePickerEx : DatePicker, then you'll get all the public properties and methods of DatePicker. If that's not working, you did something wrong . Not sure what since you didn't post what errors you are getting or any code.
|
|
|
|
|
in my application WPF/C# , i have a simple TreeView "treview1" that content for exemple:
*item1
* item1.1
*item2
* item2.1
Now i have an Class "Search.cs", and within this Class i have some methods like: advanced_serch and simpl_search , I want to be able to modified my TreeView in this two methods(advanced_serch and simpl_search).....
How i can dos this???
Please help me !!
|
|
|
|
|
What do you expect your searches to do (other than find a node label)?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
for exemple, in my Advanced_Search i want to upadate My TreeView( delete or add items).
|
|
|
|
|
Mouaici Mohamed wrote: Advanced_Search i want to upadate My TreeView( delete or add items). That makes no sense, search implies finding something, not modifying the data.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I am attempting to create a common WPF logon dialog. I have the dialog as a separate DLL which access a service and returns the user permissions. This works except the dialog displays as an independent window. I want to pass the main window to the dialog as the owner.
I call the logon dll from the main viewmodel
public void ShowLogon()
{
new IFALogon.LogonDlg().DoLogon(MainUI.AppID, "Finance NPA");
if (IFALogon.LogonVM.DBUser == null)
{
Exit();
}
else
{
}
}
How can I pass the main view to this DLL.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
In a WPF application, you have global access to the main window through the static Application.Current.MainWindow.
|
|
|
|
|
That did the trick, I now have a DLL that can be used to authenticate and authorise all our WPF apps. Thank you!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have a Treeview (telerik) using HierarchicalDataTemplate
<telerik:RadTreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding ChildNodes}">
<Border Background="{Binding BackColour}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageSource,Converter={StaticResource ImageFromAssetsConverter}}"
Width="16"
Height="16" />
<TextBlock Margin="10,0"
Text="{Binding NodeLabel}"
TextWrapping="Wrap"
VerticalAlignment="Center" />
<CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}"
Content="Default N/A" Margin="15,0,0,0"
Visibility="{Binding ShowCheckBox,Converter={StaticResource VisibilityConverter}}" />
</StackPanel>
</Border>
</HierarchicalDataTemplate>
</telerik:RadTreeView.ItemTemplate>
The user MUST select the node BEFORE checking the checkbox. Is it possible to select the node when the user checks the checkbox.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yes. Add a trigger to the TreeViewItem that says if IsKeyboardFocusWithin is true, then select the item.
|
|
|
|
|
yes pass a event treeviewitem
|
|
|
|
|
Thank you that was enough to chase down the detail.
Never underestimate the power of human stupidity
RAH
|
|
|
|