Use created DLL in a Wpf window? - c#

I have a DLL with the following code
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ApplicationCheck
{
public class ApCkr
{
#region .NET
public string Netframeworkavailable()
{
bool NETinstall;
RegistryKey k1 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client");
if (k1 == null)
{
NETinstall = false;
}
else
{
NETinstall = true;
}
return NETinstall.ToString();
}
#endregion
#region PDF
public string PDFavailable()
{
bool PDFinstall;
RegistryKey k2 = Registry.ClassesRoot.OpenSubKey(".pdf");
if (k2 == null)
{
PDFinstall = false;
}
else
{
PDFinstall = true;
}
return PDFinstall.ToString();
}
#endregion
#region IExplore
public string IEavailable()
{
bool IEversion;
string k3 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Internet Explorer").GetValue("Version").ToString();
string z = k3.Substring(0, 1);
int a = Int32.Parse(z);
if (a < 8)
{
IEversion = false;
}
else
{
IEversion = true;
}
return IEversion.ToString();
}
#endregion
#region IIS
public string IISavailable()
{
bool IISinstall;
RegistryKey k4 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\InetStp");
if (k4 == null)
{
IISinstall = false;
}
else
{
IISinstall = true;
}
return IISinstall.ToString();
}
#endregion
}
}
And a WPF window with the followig XAML code
<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="CanResizeWithGrip"
WindowStyle="None" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
Title="Window2" Height="350" Width="525">
<Grid>
<Label Content="Windows" Height="25" HorizontalAlignment="Left" Margin="12,15,0,0" Name="label1" VerticalAlignment="Top" Width="106" />
<Label Content="Edition " Height="25" HorizontalAlignment="Left" Margin="12,45,0,0" Name="label2" VerticalAlignment="Top" Width="106" />
<Label Content="Service Pack " Height="25" HorizontalAlignment="Left" Margin="12,75,0,0" Name="label3" VerticalAlignment="Top" Width="106" />
<Label Content="Version " Height="25" HorizontalAlignment="Left" Margin="12,105,0,0" Name="label4" VerticalAlignment="Top" Width="106" />
<Label Content="Processor Bits " Height="25" HorizontalAlignment="Left" Margin="12,135,0,0" Name="label5" VerticalAlignment="Top" Width="106" />
<Label Content="OS Bits " Height="25" HorizontalAlignment="Left" Margin="12,165,0,0" Name="label6" VerticalAlignment="Top" Width="106" />
<Label Content="Program Bits " Height="25" HorizontalAlignment="Left" Margin="12,195,0,0" Name="label7" VerticalAlignment="Top" Width="106" />
<TextBlock Height="21" HorizontalAlignment="Left" Margin="114,19,0,0" Name="textBlock1" Text="{Binding Path=var}" VerticalAlignment="Top" Width="249" ContextMenuOpening="textBlock1_ContextMenuOpening" />
</Grid>
</Window>
and the WPF's c# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private void textBlock1_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var NET = new ApplicationCheck.ApCkr();
textBlock1.Text = NET.Netframeworkavailable();
this.DataContext = textBlock1;
}
}
}
I researched data binding in MSDN and At stack overflow namely this - DataBinding Between a WPF GUI and a couple of ListBox/CheckBox
and others but i cannot get it right.And although stack overflow helped me utilise this in a console app.Now i have to do this in a WPF window.
Edit:I have to display the returned values from the DLL

In order to present the data from the DLL in the UI using binding, you need to have an object with public getters. In your UI DLL create a class (in mvvm design patern, this class is called 'View Model') with the public getters:
public class ApCkrVm {
public string netFrameworkAvailable {
get { return ApCkr.NetFrameworkAvailable(); }
}
public string pdfAvailable {
get { return ApCkr.PDFAvailable(); }
}
...
}
Then, in Window2 constructor, set ApCkrVm to be the DataContext:
public Window2( ) {
this.DataContext = new ApCkrVm( );
InitializeComponent( );
}
Finally, add text blocks in the XML file, binding the Text to the properties:
<TextBlock Text="{Binding Path=netFrameworkAvailable}" ... />
Some other comments:
You aren't utilizing very well the <Grid> element. You'll be better off defining ColumnDefinitions and RowDefinitions, creating a 2xn table.
ApCkr method can all be static. This class has no context.
I don't think your application could run if .net framework isn't available. If your application is running, you can safely put 'true' there.
Consider caching the values in ApCkrVm.

First add that Dll in your resources after that write following code in your wpf form xaml side.
<Window x:Class="TestWpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="220" Width="343"
xmlns:my="clr-namespace:TestWpfControlLibrary;assembly=TestWpfControlLibrary" Left="Auto" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">
this following last line is impotent. it decide you use the particular dll.
xmlns:my="clr-namespace:TestWpfControlLibrary;assembly=TestWpfControlLibrary"`
after that in xaml page within grid teg use define "my", whome you use in xmlns:my
for that use following code.
<my:UserControl1 Height="168" HorizontalAlignment="Left" Margin="10,22,0,0" VerticalAlignment="Top" Width="307" Name="login" />
after that go to form.cs page and write the following code in pageload method
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
login.Visibility = System.Windows.Visibility.Visible;
}
}
now your dll is working properly.
you can download a test application like this from following link
http://www.dotnetfoundation.somee.com/Style/DesktopApp/WPF TEST.zip

Related

How to edit an observableCollection while WPF app is running

I am making a To-do List application for one of my classes. Now I have tried in a console app to update objects (of TaskModels a type I defined) inside an observable collection and it has seemed to work. But now I am trying to update the entries inside the observable collection. I have wrote some code that I thought was going to change the value but it did not change the value in the ListBox I will include the code below for TaskModel.
Originally I was wanting to have the edit button open a new window that allowed the user to input what the wanted the task to be and then they press a button maybe called change task and it sends it back to the original window. But for simpleness at the moment I would like to change with the textbox in the user control in the main window.
I am also pretty new to making apps in WPF so this is all pretty new to me and I am trying to learn it all.
Below is my TaskModel and the only thing it does is get and set the TaskName.
namespace ToDoList.Model
{
public class TaskModel
{
private string taskName;
public string TaskName {
get { return taskName; }
set { taskName = value; }
}
}
}
I wrote the following code hoping that it would allow me to change the value of the TaskName but it does not seem to be working. Is there anyhting else that I should add to the code for it change the TaskName properly? Any tips or anything that could help me with this problem.
Below is the XAML code for my main window. It is a really simple UI, it features a ListBox which uses an ObservableCollection as its itemsource and when the listbox has a new item put into it has a checkbox to the left of it.
Below I will include the main window XAML and the C# code.
<Window x:Class="ToDoList.DemoMainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ToDoList"
mc:Ignorable="d"
Title="The To-Do List" Height="500" Width="500" FontSize="22"
Background="White">
<Grid Margin="10" Background="BlueViolet">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2">
<TextBlock>Tasks to do:</TextBlock>
<!-- <TextBlock>Blah blah</TextBlock> -->
<!-- <local:UCLabelTextBxInput x:Name="TxtUCSaveToFileLocation" Title="Save to File Location" MaxLength="50"></local:UCLabelTextBxInput> -->
<ListBox x:Name="LstBoxTasks" MinHeight="200" MaxHeight="200" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Mode=OneWay}" Content="{Binding TaskName, Mode=TwoWay}" FontSize="14"></CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<local:UCLabelTextBxInput x:Name="TxtUCEnteredTask" Title="Enter Task Here:" MaxLength="50"></local:UCLabelTextBxInput>
<Button x:Name="BtnAddTask" Click="BtnAddTask_Click" Background="Chocolate">Add Task to List</Button>
</StackPanel>
<Button Grid.Column="0" Grid.Row="1" Margin="0,10,20,0" x:Name="btnDeleteTask" Click="BtnDeleteTask_Click" Background="Chocolate">Delete Task</Button>
<Button Grid.Column="1" Grid.Row="1" Margin="20,10,0,0" Background="Chocolate" Click="BtnEditTask_Click">Edit Task</Button>
<Button Grid.Column="0" Grid.Row="2" Margin="0,10,0,10" Grid.ColumnSpan="2" Background="Chocolate" Click="BtnHelp_Click">Help</Button>
</Grid>
</Window>
Then finally here is the C# code:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ToDoList.Model;
namespace ToDoList
{
/// <summary>
/// Interaction logic for DemoMainWindow.xaml
/// </summary>
public partial class DemoMainWindow : Window
{
SaveDataModel saveDataModel = new SaveDataModel();
ObservableCollection<TaskModel> tasksModels = new ObservableCollection<TaskModel>();
public DemoMainWindow()
{
InitializeComponent();
TxtUCEnteredTask.txtLimitedInput.Text = "Do the dishes";
LstBoxTasks.ItemsSource = tasksModels;
}
private void BtnAddTask_Click(object sender, RoutedEventArgs e)
{
tasksModels.Add(new TaskModel() { TaskName = TxtUCEnteredTask.txtLimitedInput.Text });
}
private void BtnDeleteTask_Click(object sender, RoutedEventArgs e)
{
if(LstBoxTasks.SelectedItem != null)
{
tasksModels.Remove(LstBoxTasks.SelectedItem as TaskModel);
}
}
private void BtnHelp_Click(object sender, RoutedEventArgs e)
{
HelpWindow helpWindow = new HelpWindow();
helpWindow.Show();
}
private void BtnEditTask_Click(object sender, RoutedEventArgs e)
{
if (LstBoxTasks.SelectedItem != null)
{
tasksModels[LstBoxTasks.SelectedIndex].TaskName = TxtUCEnteredTask.txtLimitedInput.Text;
}
}
}
}
Your property TaskName is not calling the OnPropertyChanged() method that notifies your view that the name changed.
You can do something like that:
private string taskName;
public string TaskName {
get { return taskName; }
set {
if( value != taskName) {
taskName = value;
OnPropertyChanged("TaskName");
}
}
}
protected void OnPropertyChanged(string name) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
Remarks:
The best way to do WPF is to do MVVM.
You should check out this link : https://www.codeproject.com/Tips/806587/Basic-MVVM-Listbox-Binding-in-WPF

Setting a binding for a second window

I have two WPF windows and i want to set the context to a ViewModel but if I write:
this.DataContext = new myViewModel()
in my second windows cs it doesn't work here is my code. i have tried to place a binding in the XAML and to connect the context but when i try to debug it all i get the error code this breakpoint will not get run.
BrowseDialog.xaml
<Window x:Class="TextalkApi.BrowseDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TextalkApi"
mc:Ignorable="d"
Title="BrowseDialog" Height="248.361" Width="427.459">
<Grid>
<Button Content="Browse" HorizontalAlignment="Left" Margin="267,11,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="FileDialog" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding webUrl}" VerticalAlignment="Top" Width="244"/>
<Button Content="Save" HorizontalAlignment="Left" Margin="267,166,0,0" VerticalAlignment="Top" Width="75" Command="{Binding SaveCommand}" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label Content="{Binding errorMessage}" HorizontalAlignment="Left" Margin="10,167,0,0" VerticalAlignment="Top" RenderTransformOrigin="-5.611,10.822" Width="207" Height="19"/>
</Grid>
</Window>
BrowseViewModel
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using System.Threading;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Specialized;
using System.IO;
namespace Data
{
public class BrowseViewModel : BaseViewModel
{
#region public variables
public string webUrl { get; set; }
public string errorMessage { get; set; }
#endregion
#region Public Commands
public ICommand SaveCommand { get; set; }
#endregion
#region Constructor
public BrowseViewModel()
{
this.SaveCommand = new RelayCommand(SaveFilePath);
}
#endregion
#region Private methods
private void SaveFilePath()
{
if (File.Exists(webUrl))
{
ConfigurationManager.AppSettings.Add("WebUrl", webUrl);
}
else
{
errorMessage = "Filen existerar ej";
}
}
#endregion
}
}
BrowseDialog.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using Data;
namespace TextalkApi
{
/// <summary>
/// Interaction logic for BrowseDialog.xaml
/// </summary>
public partial class BrowseDialog : Window
{
public BrowseDialog()
{
InitializeComponent();
this.DataContext = new BrowseViewModel();
}
}
}
BaseViewModel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using PropertyChanged;
namespace Data
{
[AddINotifyPropertyChangedInterface]
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
}
}
Your XAML looks correct and I highly suspect it is the ViewModel causing it not to work. Something like this should work.
Please note that this will depend a lot on how the BaseViewModel has been implemented. If possible can you share this so I can update my answer to be correct? The below is how you should implement the properties in your ViewModel which is the DataContext of the View.
#region Properties
private string _webUrl;
public string WebUrl
{
get => _webUrl;
set
{
//This will change based on how you have implemented your BaseViewModel!
//The method name might be different, or have different parameters!
this.SetProperty(ref _webUrl, value, nameof(WebUrl));
//Call the save file path validation method...
SaveFilePath();
}
}
private string _errorMessage;
public string ErrorMessage
{
get => _errorMessage;
private set
{
//This will change based on how you have implemented your BaseViewModel!
//This method should call NotifyPropertyChange to notify the UI to update...
this.SetProperty(ref _errorMessage, value, nameof(ErrorMessage));
}
}
#endregion
In your ViewModelBase you can add a generic SetProperty method that can then handle raising the property changed event for you. Something like this:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void SetProperty<T>(ref T storage, T value, string propertyName)
{
storage = value;
RaisePropertyChangedEvent(propertyName);
}
protected void RaisePropertyChangedEvent(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
and because of the changes in the ViewModel you will also need to update the bindings in your XAML.
<Grid>
<Button Content="Browse" HorizontalAlignment="Left" Margin="267,11,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="FileDialog" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding WebUrl}" VerticalAlignment="Top" Width="244"/>
<Button Content="Save" HorizontalAlignment="Left" Margin="267,166,0,0" VerticalAlignment="Top" Width="75" Command="{Binding SaveCommand}" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label Content="{Binding ErrorMessage}" HorizontalAlignment="Left" Margin="10,155,0,0" VerticalAlignment="Top" RenderTransformOrigin="-5.611,10.822" Width="207" Height="46"/>
</Grid>

How to assign property (get; set;) to a WPF User Control?

I'm trying to learn property (get and set) in C#. I'm still new to the language and currently making a simple program using WPF with some textboxes, as you can see in the pictures.
So, here are the description :
Input : it's where user can type the input, located at MainWindow
Output 1 : it's where to see the typed string in input, located at MainWindow
Output 2 : same like Output 1, located inside tab one, at MainWindow.
Output 3 : same like Output 1, located inside tab two, still at MainWindow.
Output 4 : same like Output 1, located inside tab two, referred to local UserControl Page1.
Button : a button to "save" the input
Here are the codes :
Main Window : solution --> I added x:Name="Page1" after local:page1
<!-- MainWindow.xaml -->
<Window x:Class="TestGetSet.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestGetSet"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl x:Name="tabControl" HorizontalAlignment="Left" Height="212" Margin="37,20,0,0" VerticalAlignment="Top" Width="447">
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5">
<TextBox x:Name="output2" HorizontalAlignment="Left" Height="23" Margin="156,76,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
<Label x:Name="label2" Content="Output 2" HorizontalAlignment="Left" Margin="156,46,0,0" VerticalAlignment="Top"/>
</Grid>
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5">
<TextBox x:Name="output3" HorizontalAlignment="Left" Height="23" Margin="321,26,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<local:Page1 x:Name="Page1"/>
<Label x:Name="label3" Content="Output 3" HorizontalAlignment="Left" Margin="321,0,0,0" VerticalAlignment="Top"/>
</Grid>
</TabItem>
</TabControl>
<TextBox x:Name="input" Text=""/>
<TextBox x:Name="output1" Text=""/>
<Button x:Name="button" Content="Button" Click="button_Click"/>
<Label x:Name="label" Content="Input" HorizontalAlignment="Left" Margin="37,239,0,0" VerticalAlignment="Top"/>
<Label x:Name="label1" Content="Output 1" HorizontalAlignment="Left" Margin="364,239,0,0" VerticalAlignment="Top"/>
</Grid>
MainWindow code behind : solution --> added one line Page1.passingvalue(..)
// MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
public partial class MainWindow : Window
{
public myProperty myProp = new myProperty();
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
myProp.myData = input.Text;
output1.Text = myProp.myData;
output2.Text = myProp.myData;
output3.Text = myProp.myData;
Page1.passingvalue(myProp.myData);
}
}
Next is Page1.xaml (No changes made here for the solution)
<!-- Page1.xaml-->
<UserControl x:Class="TestGetSet.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestGetSet"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox x:Name="output4" Text=""/>
<Label x:Name="label" Content="Output 4" HorizontalAlignment="Left" Margin="92,110,0,0" VerticalAlignment="Top"/>
</Grid>
</UserControl>
Page1 code behind : solution--> deleted timer and added passingvalue
// Page1.xaml.cs
using System;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;
namespace TestGetSet
{
public partial class Page1 : UserControl
{
private Thread _receiveThread;
myProperty myProp = new myProperty();
public Page1()
{
InitializeComponent();
/*DispatcherTimer MyTimer = new DispatcherTimer();
MyTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
MyTimer.Tick += MyTimer_Tick;
MyTimer.Start();*/
}
public void passingvalue(string m)
{
output4.Text = m;
}
/*private void MyTimer_Tick(object sender, EventArgs e)
{
output4.Text = myProp.myData;
}*/
}
}
Last one, the property, simple version :
// myProperty.cs
namespace TestGetSet
{
public class myProperty
{
public string myData { get; set }
}
}
Property with INotifyPropertyChanged :
// myProperty.cs
using System.ComponentModel;
namespace TestGetSet
{
public class myProperty : INotifyPropertyChanged
{
private string _textdata;
public string myData {
get
{
return _textdata;
}
set
{
_textdata = value;
NotifyPropertyChanged("myData");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Here are the screenshots of the program window :
one
and
two
As you can see, only Output4 is empty and the other outputs still give me results even though I don't use INotifyPropertyChanged. My question is why, and how can I fix this? I wonder if it's because I'm using UserControl for Page1, which the Output4 is in. I've been looking for the answer but came up with nothing. Any help is much appreciated. Thank you.
Okay so I updated the code. It's working now. I got the reference for passingvalue from : How to Pass a Value From a Window to a UserControl in WPF
Thank you.
Your issue here is that you misstakes the MainWindow's myProperty and Page1's myProperty.
In Page1, you set the output4.Text using myProp.myData which is wrong because myProp is the Page1's myProp which is never updated.
If you pass the MainWindow's reference to Page1 and instead write something like output4.Text = myMainWindowReferenc.myProp.myData; it will work as intended.
You could also add setting to output4 in your button_Click function by naming the Page1 in the XAML:
private void button_Click(object sender, RoutedEventArgs e)
{
myProp.myData = input.Text;
output1.Text = myProp.myData;
output2.Text = myProp.myData;
output3.Text = myProp.myData;
Page1.output4.Text = myProp.myData;
}

WPF databinding cannot get data

I am writting a login window using WPF and C#,but got stuck when I attempted to retrive username from the TextBox.The class property which I bind to the TextBox always get a null value,and I cannot figure out why.
MainWindow.xaml
<Window x:Class="Databinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="256*"/>
<ColumnDefinition Width="261*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Username" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontSize="20"/>
<Label Grid.Row="1" Grid.Column="0" Content="Password" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontSize="20"/>
<Button Content="Confirm" Click="Confirm_Click" IsDefault="True" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" FontSize="20"/>
<Button Content="Cancel" Click="Cancel_Click" IsCancel="True" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" FontSize="20"/>
<TextBox x:Name="TextUsername" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,0,0,0" TextWrapping="Wrap" VerticalAlignment="Stretch" ToolTip="Enter your username"/>
<PasswordBox x:Name="Password" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" ToolTip="Enter your password"/>
</Grid>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace Databinding
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Confirm_Click(object sender, RoutedEventArgs e)
{
Login LoginInfo = new Login();
Binding bindingLogin = new Binding();
bindingLogin.Source = LoginInfo;
bindingLogin.Path = new PropertyPath("Username");
bindingLogin.Mode = BindingMode.TwoWay;
bindingLogin.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(this.TextUsername, TextBox.TextProperty, bindingLogin);
if(LoginInfo.Username=="admin" && this.Password.Password=="admin")
{
MessageBox.Show("Welcome!","Login Status");
}
else
{
MessageBox.Show("Something is wrong!","Login Status");
}
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
public class Login:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string username;
public string Username
{
get
{
return username;
}
set
{
username = value;
if(this.PropertyChanged!=null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Username"));
}
}
}
}
}
I know databinding is not a wise option in this case,and I can get things done more efficently by just using
string Username = this.TextUsername.Text
Anyway,this is a some demo,and I have to use databinding in my project.What's wrong with my code?
At the point you attach your binding, Login.Username is null, and since the binding is two way WPF will update your textbox to null to match.
Bindings are supposed to be active all the time, and declared in the XAML, rather than bound at the point you want data. You are defeating the object of using bindings. Like you say, it would be far easier to just grab the text directly if you are going to do it explicitly.
The problem is when user entering/changing Text in the TextUsername, your binding is not there yet, (your binding will only be there after the Confirm_Click, and will be reset with each Confirm_Click) so you need to move it to the constructor.
namespace Databinding
{
public partial class MainWindow : Window
{
Login LoginInfo;
public MainWindow()
{
InitializeComponent();
LoginInfo = new Login();
Binding bindingLogin = new Binding();
bindingLogin.Source = LoginInfo;
bindingLogin.Path = new PropertyPath("Username");
bindingLogin.Mode = BindingMode.TwoWay;
bindingLogin.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(this.TextUsername, TextBox.TextProperty, bindingLogin);
}
//... rest
}
}
And you could easily apply the binding in XAML, and avoid all of these code behind if you can set the DataContext of your window correctly e.g.:
<TextBox x:Name="TextUsername" Text="{Binding Username}" ..../>
The below code will works, not sure if it's a good practice thou:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace Databinding
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Login LoginInfo;
Binding bindingLogin;
public MainWindow()
{
InitializeComponent();
LoginInfo = new Login();
bindingLogin = new Binding();
bindingLogin.Source = LoginInfo;
bindingLogin.Path = new PropertyPath("Username");
bindingLogin.Mode = BindingMode.TwoWay;
bindingLogin.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
}
private void Confirm_Click(object sender, RoutedEventArgs e)
{
BindingOperations.SetBinding(this.TextUsername, TextBox.TextProperty, bindingLogin);
if(LoginInfo.Username=="admin" && this.Password.Password=="admin")
{
MessageBox.Show("Welcome!","Login Status");
}
else
{
MessageBox.Show("Something is wrong!","Login Status");
}
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
public class Login:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string username;
public string Username
{
get
{
return username;
}
set
{
username = value;
if(this.PropertyChanged!=null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Username"));
}
}
}
}
}

Dynamically set TextBlock's text binding

I am attempting to write a multilingual application in Silverlight 4.0 and I at the point where I can start replacing my static text with dynamic text from a SampleData xaml file. Here is what I have:
My Database
<SampleData:something xmlns:SampleData="clr-namespace:Expression.Blend.SampleData.MyDatabase" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<SampleData:something.mysystemCollection>
<SampleData:mysystem ID="1" English="Menu" German="Menü" French="Menu" Spanish="Menú" Swedish="Meny" Italian="Menu" Dutch="Menu" />
</SampleData:something.mysystemCollection>
</SampleData:something>
My UserControl
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="Something.MyUC" d:DesignWidth="1000" d:DesignHeight="600">
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MyDatabase}}">
<Grid Height="50" Margin="8,20,8,0" VerticalAlignment="Top" d:DataContext="{Binding mysystemCollection[1]}" x:Name="gTitle">
<TextBlock x:Name="Title" Text="{Binding English}" TextWrapping="Wrap" Foreground="#FF00A33D" TextAlignment="Center" FontSize="22"/>
</Grid>
</Grid>
</UserControl>
As you can see, I have 7 languages that I want to deal with. Right now this loads the English version of my text just fine. I have spent the better part of today trying to figure out how to change the binding in my code to swap this out when I needed (lets say when I change the language via drop down).
It sounds like you're looking for code like this:
Title.SetBinding(TextProperty, new Binding { Path = new PropertyPath(language) });
All it does is create a new Binding for the language you requested and use it to replace the old binding for the Title's Text property.
You are going about this the wrong way. Best practice for localization in Silverlight is to use resource files holding the translated keywords. Here is some more info about this:
http://msdn.microsoft.com/en-us/library/cc838238%28VS.95%29.aspx
EDIT:
Here is an example where I use a helper class to hold the translated strings. These translations could then be loaded from just about anywhere. Static resource files, xml, database or whatever. I made this in a hurry, so it is not very stable. And it only switches between english and swedish.
XAML:
<UserControl x:Class="SilverlightApplication13.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SilverlightApplication13"
mc:Ignorable="d"
d:DesignWidth="640"
d:DesignHeight="480">
<UserControl.Resources>
<local:TranslationHelper x:Key="TranslationHelper"></local:TranslationHelper>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<StackPanel>
<TextBlock Margin="10"
Text="{Binding Home, Source={StaticResource TranslationHelper}}"></TextBlock>
<TextBlock Margin="10"
Text="{Binding Contact, Source={StaticResource TranslationHelper}}"></TextBlock>
<TextBlock Margin="10"
Text="{Binding Links, Source={StaticResource TranslationHelper}}"></TextBlock>
<Button Content="English"
HorizontalAlignment="Left"
Click="BtnEnglish_Click"
Margin="10"></Button>
<Button Content="Swedish"
HorizontalAlignment="Left"
Click="BtnSwedish_Click"
Margin="10"></Button>
</StackPanel>
</Grid>
</UserControl>
Code-behind + TranslationHelper class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.ComponentModel;
namespace SilverlightApplication13
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
//Default
(this.Resources["TranslationHelper"] as TranslationHelper).SetLanguage("en-US");
}
private void BtnEnglish_Click(object sender, RoutedEventArgs e)
{
(this.Resources["TranslationHelper"] as TranslationHelper).SetLanguage("en-US");
}
private void BtnSwedish_Click(object sender, RoutedEventArgs e)
{
(this.Resources["TranslationHelper"] as TranslationHelper).SetLanguage("sv-SE");
}
}
public class TranslationHelper : INotifyPropertyChanged
{
private string _Contact;
/// <summary>
/// Contact Property
/// </summary>
public string Contact
{
get { return _Contact; }
set
{
_Contact = value;
OnPropertyChanged("Contact");
}
}
private string _Links;
/// <summary>
/// Links Property
/// </summary>
public string Links
{
get { return _Links; }
set
{
_Links = value;
OnPropertyChanged("Links");
}
}
private string _Home;
/// <summary>
/// Home Property
/// </summary>
public string Home
{
get { return _Home; }
set
{
_Home = value;
OnPropertyChanged("Home");
}
}
public TranslationHelper()
{
//Default
SetLanguage("en-US");
}
public void SetLanguage(string cultureName)
{
//Hard coded values, need to be loaded from db or elsewhere
switch (cultureName)
{
case "sv-SE":
Contact = "Kontakt";
Links = "Länkar";
Home = "Hem";
break;
case "en-US":
Contact = "Contact";
Links = "Links";
Home = "Home";
break;
default:
break;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}

Categories