Setting up datacontext of the user control in client application in WPF - c#

I have created a third party user control and now want to use it in a client application. I am having a problem with setting the DataContext to the control.
UserControl :-
<Grid>
<DataGrid x:Name="dataGrid" Width="400" Height="400" ItemsSource="{Binding DataTableSource}"/>
</Grid>
Code behind:-
public partial class CustomGridControl : UserControl
{
public CustomGridControl()
{
InitializeComponent();
this.DataContext = this;
}
public DataTable DataTableSource
{
get
{
return (DataTable)GetValue(GridSource);
}
set
{
SetValue(GridSource, value);
}
}
public static readonly DependencyProperty GridSource = DependencyProperty.Register("DataTableSource", typeof(DataTable), typeof(CustomGridControl), new PropertyMetadata(null));
}
How do I set DataTableSource in the client application?
<Grid>
<controls:CustomGridControl Name="myCustGrid" />
</Grid>
public MainWindow()
{
InitializeComponent();
ds = provider.GetDataSet();
table = ds.Tables[0];
//I have to set the table as DataTableSource. Here I am unable to access DataTableSource.
}
I am unable to access myCustGrid.DataTableSource. It says CustomGridControl does not contain a definition for DataTableSource. Why?

I've tried your custom as inheriting from Grid:
public partial class CustomGridControl : Grid
{
public DataTable DataTableSource
{
get
{
return (DataTable)GetValue(GridSource);
}
set
{
SetValue(GridSource, value);
}
}
public static readonly DependencyProperty GridSource = DependencyProperty.Register("DataTableSource", typeof(DataTable), typeof(CustomGridControl), new PropertyMetadata(null));
}
This the xaml:
<local:CustomGridControl x:Name="testCustomGrid" />
And i am able to use from codebehing like this:
testCustomGrid.DataTableSource = new DataTable("dtName");
I've been able to inherit from a UserControl too:
/// <summary>
/// Interaction logic for CustomGridControl.xaml
/// </summary>
public partial class CustomGridControl : UserControl, INotifyPropertyChanged
{
public CustomGridControl()
{
InitializeComponent();
}
private DataTable _DataTableSource;
public DataTable DataTableSource
{
get { return _DataTableSource; }
set
{
_DataTableSource = value;
PropertyChanged(this, new PropertyChangedEventArgs("DataTableSource"));
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public DataTable DataTableSourceVersion2
{
get { return (DataTable)GetValue(DataTableSourceVersion2Property); }
set { SetValue(DataTableSourceVersion2Property, value); }
}
// Using a DependencyProperty as the backing store for DataTableSourceVersion2. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataTableSourceVersion2Property =
DependencyProperty.Register("DataTableSourceVersion2", typeof(DataTable), typeof(CustomGridControl), new PropertyMetadata(null));
}
And this is the XAML:
<local:CustomGridControl DataTableSource="" DataTableSourceVersion2=""/>
So, both version of the DataSourceTable are available.

Related

Nested UserControl(s) throw "Property was already registered by"

I have this UserControl xaml:
<Grid>
<Separator x:Name="sep" VerticalAlignment="Center" Height="10" />
</Grid>
and its code behind where I define a DependencyProperty to edit the line color:
public partial class SeparatorLineText : UserControl
{
public static DependencyProperty? ColorProperty;
private PropertyMetadata meta = new PropertyMetadata(propertyChangedCallback: ColorChanged);
public SeparatorLineText()
{
ColorProperty = DependencyProperty.Register("MyColor",
typeof(Brush),
typeof(SeparatorLineText),
meta);
InitializeComponent();
}
public Brush MyColor
{
get { return (Brush)base.GetValue(ColorProperty); }
set { base.SetValue(ColorProperty, value); }
}
private static void ColorChanged(object d, DependencyPropertyChangedEventArgs e)
{
((SeparatorLineText)d).OnColorChanged(e);
}
protected virtual void OnColorChanged(DependencyPropertyChangedEventArgs e)
{
sep.Background = (Brush)e.NewValue;
}
}
Then I have this other UserControl which has SeparatorLineText inside:
<UserControl x:Class="MySubWindow"
...
>
<Grid>
<control:SeparatorLineText MyColor="Red"/>
</Grid>
Finally, in MainWindow.xaml I include MySubWindow which has SeparatorLineText inside:
<control:MySubWindow x:Name="MyTab" VerticalAlignment="Top" Width="1280"/>
When i run the project it displays my custom separator correctly, but in the MainWindow's xaml designer it doesn't load correctly saying: "MyColor property was already registered by SeparatorLineText"
I've already read the other topics about this but I didn't find a solution.
Dependency properties only need to be registered once. In your situation it is registered every time the constructor is called. You can solve this with a static constructor:
public static DependencyProperty ColorProperty;
// You can also make this field static, since it is not bound to a specific instance.
private static PropertyMetadata meta = new PropertyMetadata(propertyChangedCallback: ColorChanged);
static SeparatorLineText()
{
ColorProperty = DependencyProperty.Register("MyColor",
typeof(Brush),
typeof(SeparatorLineText),
meta);
}
public SeparatorLineText()
{
InitializeComponent();
}
Or initialize it directly:
public static DependencyProperty ColorProperty =
DependencyProperty.Register("MyColor",
typeof(Brush),
typeof(SeparatorLineText),
meta);
// You can also make this field static, since it is not bound to a specific instance.
private static PropertyMetadata meta = new PropertyMetadata(propertyChangedCallback: ColorChanged);
public SeparatorLineText()
{
InitializeComponent();
}

AvaloniaUI StyledProperty and MVVM binding doesn't update

I'm using AvaloniaUI and making a UserControl with a StyledProperty using. I'm using MVVM.
The problem is that when I have a Binding to my styledproperty but it doesn't update.
I want to use the UserControl like this <Comp:ValueTextBlock VariableName="{Binding RobotSettingsModel.Robot_SP}"/> where VariableName uses a binding to a model that is created in the ViewModel.
The problem is that I can't seem to get the StyledProperty to work when I use a binding. When I set VariableName directly in the view it does work
// this works
<Comp:ValueTextBlock VariableName="PLC_U1_Robot_SP"/>
// this doesn't
<Comp:ValueTextBlock VariableName="{Binding RobotSettingsModel.Robot_SP}" DescriptionLocation="Left"/>
What am I doing wrong here?
The code-behind for my UserControl ValueTextBlock looks something like this:
public class ValueTextBlock : UserControl
{
private ValueTextBlockVm _viewModel;
#region --- Variable name properties ---
public static readonly StyledProperty<string> VariableNameProperty = AvaloniaProperty.Register<ValueTextBlock, string>(nameof(VariableName), defaultBindingMode: BindingMode.TwoWay, defaultValue: "UNKNOWN DProperty");
public string VariableName
{
get { return _viewModel.vmVariableName; } // the property is used in the ViewModel
set { _viewModel.vmVariableName = value; }
}
#endregion
#region constructor
public ValueTextBlock()
{
this.InitializeComponent();
// create new instance of viewmodel and attach it as DataContext
_viewModel = new ValueTextBlockVm();
this.DataContext = _viewModel;
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
#endregion
}
In the ViewModel for ValueTextBlock vmVariableName is done like this:
private string _vmVariableName = "UKN";
public string vmVariableName
{
get => _vmVariableName;
set => this.RaiseAndSetIfChanged(ref _vmVariableName, value);
}
When i use this UserControl and directly set VariableName in view it works, but when I use Binding it doesn't work.
This is my view:
<StackPanel>
<Comp:ValueTextBlock VariableName="PLC_U1_Robot_SP"/> <!-- directly setting VariableName works -->
<Comp:ValueTextBlock VariableName="{Binding RobotSettingsModel.Robot_SP}" DescriptionLocation="Left"/> <!-- binding VariableName to a model doesn't work -->
<TextBlock Text="{Binding RobotSettingsModel.Robot_SP}"/> <!-- binding normal text to a model does work-->
</StackPanel>
Code-behind for the view
public class ucRobotSettings : UserControl
{
private ucRobotSettingsVm _viewModel;
#region properties
public string Prefix
{
get { return _viewModel.vmPrefix; }
set { _viewModel.vmPrefix = value; }
}
public static readonly StyledProperty<string> PrefixProperty = AvaloniaProperty.Register<ucRobotSettings, string>(nameof(Prefix));
#endregion
#region constructor
public ucRobotSettings()
{
this.InitializeComponent();
// create new instance of viewmodel and attach it as DataContext
_viewModel = new ucRobotSettingsVm();
this.DataContext = _viewModel;
this.AttachedToVisualTree += ucRobotSettings_AttachedToVisualTree;
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
#endregion
private void ucRobotSettings_AttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e)
{
_viewModel.OnAttachedToVisualTree();
}
}
In the ViewModel a new RobotSettingsModel is made, this is what I want to bind to in the View
public class ucRobotSettingsVm : ViewModelBase
{
#region --- properties ---
private string _vmPrefix;
public string vmPrefix
{
get => _vmPrefix;
set => this.RaiseAndSetIfChanged(ref _vmPrefix, value);
}
private RobotSettingsModel _RobotSettingsModel = new RobotSettingsModel();
public RobotSettingsModel RobotSettingsModel
{
get => _RobotSettingsModel;
set => this.RaiseAndSetIfChanged(ref _RobotSettingsModel, value);
}
#endregion
public ucRobotSettingsVm() { }
public void OnAttachedToVisualTree()
{
// don't update if the prefix hasn't changed
if (RobotSettingsModel.Prefix != vmPrefix) RobotSettingsModel.Prefix = vmPrefix;
}
}
The model that is used in ucRobotSettings looks like this:
public class RobotSettingsModel : ReactiveObject
{
// unit prefix.
private string _Prefix;
public string Prefix { get => _Prefix; set { this.RaiseAndSetIfChanged(ref _Prefix, value); NotifyPropertyChanged(); } }
// values
private string _Robot_SP = "UKN";
public string Robot_SP { get => _Robot_SP; set => this.RaiseAndSetIfChanged(ref _Robot_SP, value); }
public RobotSettingsModel()
{ }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = "")
{
if (propertyName == "Prefix") // only update when property "Prefix changes"
{
Robot_SP = Prefix + "_" + nameof(Robot_SP);
// inform outside outside world the complete class has PropertyChanged
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}

Issue with databinding to class?

I'm having an issue with databinding a textblock to a custom prop inside another class, what am I doing wrong?
mainpage:
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
InitializeLanguage();
}
private void InitializeLanguage()
{
LanguageHelper lh = new LanguageHelper();
// this.TitlePanel.DataContext = lh;
txtTitle.DataContext = lh;
}
}
databinding:
<TextBlock x:Name="txtTitle"
Text="{Binding homepage_subheading}"
Style="{StaticResource PhoneTextNormalStyle}"
Foreground="White"
Margin="12,0"/>
LanguageHelper class:
public class LanguageHelper
{
public String homepage_subheading;
public void changeLanguage()
{
if (true)
{
//english
homepage_subheading = "This is the top / sub Heading";
}
}
}
You don't have a property but a public field, and the databinding engine only works on properties.
So you need to change your class:
public String homepage_subheading { get; set; }
If you want to also notify the UI with your changes of your properties your LanguageHelper should implement the INotifyPropertyChanged interface and fire the PropertyChange event when you modify your properties.
You should declare some dependency property or use INotifyPropertyChanged, I would like to use a dependency property:
public class LanguageHelper : DependencyObject {
public static DependencyProperty Hompage_subheadingProperty =
DependencyProperty.Register("Homepage_subheading", typeof(string), typeof(LanguageHelper));
public String Homepage_subheading {
get { return (string) GetValue(Homepage_subheadingProperty);}
set { SetValue(Homepage_subheadingProperty, value);}
}
}
Note about the naming convention in C#, all properties should have first letter capitalized.

Alter hosted Winform control from ViewModel

Sorry to be cliche... but I'm pretty new to WPF and MVVM so I'm not sure how to handle this properly. I have a WinForms control within one of my views that I need to modify in it's code behind when an event is raised in the ViewModel. My view's datacontext is inherited so the viewmodel is not defined in the views constructor. How would I go about properly handling this? I am not using any frameworks with built in messengers or aggregators. My relevant code is below. I need to fire the ChangeUrl method from my ViewModel.
EDIT: Based on the suggestion from HighCore, I have updated my code. I am still not able to execute the ChangeUrl method however, the event is being raised in my ViewModel. What modifications need to be made??
UserControl.xaml
<UserControl ...>
<WindowsFormsHost>
<vlc:AxVLCPlugin2 x:Name="VlcPlayerObject" />
</WindowsFormsHost>
</UserControl>
UserControl.cs
public partial class VlcPlayer : UserControl
{
public VlcPlayer()
{
InitializeComponent();
}
public string VlcUrl
{
get { return (string)GetValue(VlcUrlProperty); }
set
{
ChangeVlcUrl(value);
SetValue(VlcUrlProperty, value);
}
}
public static readonly DependencyProperty VlcUrlProperty =
DependencyProperty.Register("VlcUrl", typeof(string), typeof(VlcPlayer), new PropertyMetadata(null));
private void ChangeVlcUrl(string newUrl)
{
//do stuff here
}
}
view.xaml
<wuc:VlcPlayer VlcUrl="{Binding Path=ScreenVlcUrl}" />
ViewModel
private string screenVlcUrl;
public string ScreenVlcUrl
{
get { return screenVlcUrl; }
set
{
screenVlcUrl = value;
RaisePropertyChangedEvent("ScreenVlcUrl");
}
}
WPF does not execute your property setter when you Bind the property, instead you must define a Callback method in the DependencyProperty declaration:
public string VlcUrl
{
get { return (string)GetValue(VlcUrlProperty); }
set { SetValue(VlcUrlProperty, value); }
}
public static readonly DependencyProperty VlcUrlProperty =
DependencyProperty.Register("VlcUrl", typeof(string), typeof(VlcPlayer), new PropertyMetadata(null, OnVlcUrlChanged));
private static void OnVlcUrlChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var player = obj as VlcPlayer;
if (obj == null)
return;
obj.ChangeVlcUrl(e.NewValue);
}
private void ChangeVlcUrl(string newUrl)
{
//do stuff here
}

change the MVVM WPF listview items on window initialization

I have a problem with listView initializations. The .xaml part of the listView is as below,
<ListView x:Name="categoryListView" HorizontalAlignment="Left" Width="129" Height="180"
ItemsSource="{Binding Path=RecordModel.CategoryList}"
DisplayMemberPath="RecordModel.CategoryList"
SelectedValue="{Binding Path=RecordModel.RecordTitle}"
VerticalAlignment="Top">
I have a list of String paths in RecordModel.CategoryList but I need to change the list at window initialization. Part of the view-model is below. Where can I add the code to change the list so the listView gets the changed list items at start?
public class MainWindowViewModel : ViewModelBase
{
...
private RecordModel _recordModel;
private ICommand _addCategoryCommand;
...
public MainWindowViewModel()
{
_recordModel = new RecordModel();
}
public RecordModel RecordModel
{
get { return _recordModel; }
set { _recordModel = value; }
}
...
public ICommand AddCategoryCommand
{
get
{
if (_addCategoryCommand == null)
_addCategoryCommand = new AddCat ();
return _addCategoryCommand;
}
}
public class AddCat : ICommand
{
public bool CanExecute(object parameter) { return true; }
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
MainWindowViewModel mainWindowViewModel = (MainWindowViewModel)parameter;
...
//Do things with mainWindowViewModel and the variables it has
}
...
This is the reason that ViewModels exist: so that they can transparently convert values from the Model to values more appropriate for binding.
You should expose a CategoryList property on the MainWindowViewModel and bind directly on that. You can then populate it by processing the values of RecordModel.CategoryList in the RecordModel property setter:
public class MainWindowViewModel : ViewModelBase
{
private RecordModel _recordModel;
public MainWindowViewModel()
{
RecordModel = new RecordModel(); // set the property not the field
}
public RecordModel RecordModel
{
get { return _recordModel; }
set {
_recordModel = value;
// populate CategoryList here from value.CategoryList
}
}
public UnknownType CategoryList { get; }
}

Categories