FAQ Database Discussion Community


WPF. Change DataContext on event binding to access code-behind on a MVVM project

wpf,mvvm,code-behind,datacontext
i'm developing a WPF application with MVVM. At the XAML code i have a Grid with its DataContext pointing to a ViewModel, and i need to know if it is possible to change the DataContext at runtime to access an event at its code-behind. Code-behind for the view: public partial...

Can't access datacontext of parent

c#,windows-phone-8,binding,windows-phone-8.1,datacontext
Basically what i have is a ListBox with ContextMenu <ListBox Margin="2,0,0,0" Grid.Row="1" ItemsSource="{Binding MyCollection}"> <ListBox.ItemTemplate> <DataTemplate> <Button Style="{StaticResource NoVisualButton }" Tag="{Binding ID}" Width="430" toolkit:TiltEffect.IsTiltEnabled="True" Margin="0,0,0,12" Click="OnSelectWorkOutItemClick"> <StackPanel> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="delete" Tag="{Binding ID}"...

Is it possible to have a make a generic method which can take the place of three methods with a System.Data.Linq.Mapping.FunctionAttribute?

c#,linq,entity-framework,datacontext,function-attributes
I have three methods in my datacontext class which all call slightly different stored procedures having different behaviours; APPEND, UPDATE, OVERWRITE. However all three methods have in essence the same code. The only difference is the "Name" property of the System.Data.Linq.Mapping.FunctionAttribute which decorates the methods. [Function(Name = "import.usp_MyData_ProcessImportAppend", IsComposable =...

How can I Check how many queries a LinqToSql DataContext ran?

c#,.net,linq-to-sql,datacontext
Is there any way I can check how many DB queries a DataContext ran? I know I can run an sql profiler but I want to analyze a big project with hundreds of queries. If not, is there any useful event that I can register to and count them? I'm...

Is it possible to pass current DataContext as ContructorParameter of ObjectDataProvider?

c#,wpf,datatemplate,datacontext,objectdataprovider
It would be very useful to pass DataContext as parameter for DataTemplate: <DataTemplate> <DataTemplate.Resources> <ObjectDataProvider x:Key="ServiceDataProvider" ObjectType="{x:Type control:ServiceLayout}"> <ObjectDataProvider.ConstructorParameters> /*here could be DataContext..but how?*/ </ObjectDataProvider.ConstructorParameters> </ObjectDataProvider> </DataTemplate.Resources> <ContentPresenter Content="{Binding Source={StaticResource ServiceDataProvider}}" />...

Binding issue in UserControl contained in a ListView

c#,xaml,data-binding,datacontext,windows-universal
My app structure is as follows: MainPage.xaml has a ListView that has it's ItemSource set to a CollectionViewSource which is populated in code-behind: MainPage.xaml <Page.Resources> ... <CollectionViewSource x:Key="src" IsSourceGrouped="True" /> ... </Page.Resources> <Grid> ... <ListView ItemsSource="{Binding Source={StaticResource src}}" SelectionMode="None" ItemTemplate="{StaticResource processTemplate}" ItemContainerStyle="{StaticResource ListViewItemStyle}"> <ListView.GroupStyle> <GroupStyle...

How to set the DataContext of a KeyBinding to a specific ViewModel?

c#,wpf,xaml,mvvm,datacontext
This seems like such a basic question but after hours of searching around and not figuring out what I'm doing wrong I decided it's time to ask for help! I'm new to WPF and the MVVM pattern, but am trying to create an application that has several windows you can...

Controller with same structure not working using an interface

c#,asp.net-mvc,interface,datacontext
I have created 8 models and controllers respectively for my Project using the same structure, however now this structure is not working. It is giving me this error: The current type, HelpDesk.Contracts.Repositories.IRepositoryBase`1[HelpDesk.Model.Knowledgebase], is an interface and cannot be constructed. Are you missing a type mapping? Here is my interface: namespace...

WPF use one ViewModel for multiple UserControls

wpf,binding,user-controls,viewmodel,datacontext
I've searched a lot for an answer for this question, but couldn't quite find an answer. I have my main Window and 4 different UserControls. The Window is constructed from all 4 UserControls. I have a class which is the Window's ViewModel. I wish to set the DataContext of the...

WPF binding DataGrid to CollectionViewSource: working via DataContext, empty via ItemsSource; difference?

wpf,data-binding,datagrid,datacontext,itemssource
Consider the following XAML: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:WpfApplication1" DataContext="{Binding Source={x:Static c:ViewModel.Instance}}" > <Grid> <DataGrid DataContext="{Binding ItemsViewSource}" ItemsSource="{Binding}" /> </Grid> and the view model: public class ItemViewModel { public string FirstName { get; set; } public string LastName { get; set; } }...

Bind to property before property is initialized

c#,wpf,data-binding,datacontext
I have a class Project that I'm loading via serialization. I'm storing an instance of this class in a property LoadedProject. I'm attempting to bind a label to a property of Project called ProjectName. Until serialization is complete LoadedProject is null. I understand I have to set the DataContext for...

WPF: Access parent DataTemplate from DataContext

wpf,xaml,datatemplate,datacontext
I have the below situation: <DataTemplate DataType="{x:Type tra:Presenter}"> <DockPanel> <GroupBox> <ie:DifferentControl DataContext="{Binding LocationSelectorPresenter}" Visibility="{Binding PropertyOnTraPresenter, Converter={StaticResource boolToVis}}"/> </GroupBox> </DockPanel> </DataTemplate> How do I access a property on the DataTemplate's datatype, from something that has it's own DataContext? I've attempted using FindAncestor as below, but with no luck. (This came from...

MVVM Handling PasswordBox from codebehind

mvvm,code-behind,datacontext,passwordbox
I know about those MVVM purists and PasswordBox binding problem but I'm handling my login via codebehind and it works but for the life of me I can't get over using the DataContext from codebehind. This event is attached to the PasswordChanged event of my passwordbox. Can this be handled...

Change DataContext of IQueryable

c#,multithreading,linq-to-sql,task-parallel-library,datacontext
I am working on a project that uses a static global DataContext (Which is not recommended but extremely hard to change at this point). I currently need to increase the performance of some parts by parallelizing some functions that are independent. Since DataContext is not thread-safe, I cannot use it...

WPF Data Validation inside of your DataContext?

c#,wpf,validation,xaml,datacontext
I'm using a custom ValidationRule to validate the data entered into a TextBox. <TextBox> <Binding Path="Name" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <util:AlphanumericValidationRule/> <util:StringLengthIfEnteredRule Min="1"/> </Binding.ValidationRules> </Binding> </TextBox> Which works fine to verify that only an alphanumeric string is entered, and it's length is greater than or equal to 1. The issue is that...

Reflecting over IDbSets using base class

c#,reflection,datacontext,dbset
I have a DataContext derived class that has many IDbSets that are subclasses of a base class: public class BaseClass { public int Id {get; set;} public int Length {get; set;} public int Height {get; set;} } public class Derived1 : BaseClass { public string SomeProperty {get; set} } public...