|
thanx for ur suggestions...
|
|
|
|
|
I am newbie of WPF and regardless that I am starting my own project in order to learn more everyday.
Before I ask the question I will explain you what I did so far.
Mainwindow.xaml have:
"Button" =
private void ModifyButton_Click(object sender, RoutedEventArgs e)
{
Window1 win2 = new Window1();
win2.ShowDialog();
}
Window1.xaml have:
"image"
"Buttonlink1" "Buttonlink2"
Ok that´s all.
What I want do now is this:
From the Mainwindow.xaml for the button click of "button" I want send 3 path (variables) for the "image" "buttonlink1" and "Buttonlink2" situated in Window1.xaml.
The idea is to have many buttons in the Mainwindow.xaml and each one represent a fix for a software error, this buttons will keep different data. In the "image" I have to send the path of a .jpg that will show an error dialog box. In the "buttonlink1" I have to send the path of a .bat fix that will fix the issue. In the "buttonlink2" I have to send the path of a .bat that will remotely resolve the issue.
So my question is: How i can send this 3 variables (path) from Mainwindow.xaml to window1.xaml? and how "image" "buttonlink1" "buttonlink2" will read it and execute it?
Thank you in advance for your help, I am sorry for eventual grammar errors. If something is not clear, just let me know.
Giuseppe Colello
Newbie WPF/C#
modified 10-Feb-13 14:54pm.
|
|
|
|
|
In your ModifyButton_Click you can detect the sender which is an object, cast the object as a button and get the buttons names. (sender as Button).name
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
May you write an example for me? Sorry
|
|
|
|
|
I have this Image Button class:
public class ImageButton : Button
{
public enum Direction
{
Horizontal,
Verticle
}
StackPanel panel = null;
Image _Image = null;
TextBlock _TextBlock = null;
private Direction _Layout = Direction.Horizontal;
public Direction Layout
{
get { return _Layout; }
set
{
if (_Layout != value)
{
_Layout = value;
panel.Orientation = _Layout == Direction.Horizontal ? System.Windows.Controls.Orientation.Horizontal : System.Windows.Controls.Orientation.Vertical;
}
}
}
public string Text
{
get
{
if (_TextBlock != null)
return _TextBlock.Text;
else
return String.Empty;
}
set
{
if (_TextBlock != null)
_TextBlock.Text = value;
}
}
public ImageSource Image
{
get
{
if (_Image != null)
return _Image.Source;
else
return null;
}
set
{
if (_Image != null)
_Image.Source = value;
}
}
public double ImageWidth
{
get
{
if (_Image != null)
return _Image.Width;
else
return double.NaN;
}
set
{
if (_Image != null)
_Image.Width = value;
}
}
public double ImageHeight
{
get
{
if (_Image != null)
return _Image.Height;
else
return double.NaN;
}
set
{
if (_Image != null)
_Image.Height = value;
}
}
public ImageButton()
{
_Image = new Image();
_TextBlock = new TextBlock();
panel = new StackPanel();
panel.Orientation = System.Windows.Controls.Orientation.Horizontal;
panel.Children.Add(_Image);
panel.Children.Add(_TextBlock);
this.Content = panel;
}
}
This works great, except for one thing. When I set the Layout to Verticle...
<controls:ImageButton Grid.Row="1"
Grid.Column="0"
Image="Media/Images/customer_16.png"
ImageHeight="16"
ImageWidth="16"
Text="Test"
Layout="Verticle"
Height="55"
Width="100"
Command="{Binding OpenCustomerSelectionCommand}"/>
...the Layout property does not show the enum values. If I set it like I did above, it works, but I'd like the enaum values to appear.
Not sure how to do this. Can someone help me out here?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
|
Maybe data flow isn't the right question, maybe application organization describes my quandary a little better. Here is what I am trying to do and could stand a bit of help getting there.
The app I am creating basically sits between two pieces of equipment and provides data management and communications management between the two. On one side is a piece of equipment that views its world as dealing with a single entity that provides a go no-go result. The other side is a piece of gear that can have of measurements to keep up with. Essentially all this data must be gathered, analyzed and a go no-go be sent. One side expects a TCP/IP server the other is a GPIB bus master. The comms are handled as that was the easy part.
Where I am a bit stuck is gluing all this together. The GPIB code and the logic to handle the data is done and runs as a task. The TCP/IP server is done and it runs as a task. I need to add one more piece of logic to handle the decision making plus have a UI that needs to be kept up to date.
Right now I have test programs for testing the comms etc. There I have the UI registering event handlers to get notifications of something happening. I use EventWaitHandles to notify the thin interface layers that sit about the comms. All works great! But when I put all this together..............???
I want areas of functionality as loosely coupled as I can for better reuse. In the UNIX world this is simple but in this world, not so much. My inclination is let the MainWindow code behind kick off each task and the tasks expose public methods or permit registering event handlers. But something about this doesn't feel right. In the UNIX world I would have started POSIX threads to handle each functional area and used message queues to facilitate data flow. That way synchronous functionality is grouped and asynchronous is grouped. But the Windows world doesn't seem to operate that way. What I have seen is all the classes/objects are gathered into a single app and there is a lots of "peeking" over the wall to do things which I think tends to make things very tightly coupled.
Suggestions?
TIA,
Doug
|
|
|
|
|
|
Explain why the method works so crooked add to the collection and display DataGrid.Ispolzuyu binding in XAML
<Window x:Class="bild.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:bild"
Title="MainWindow" Height="519" Width="525">
<Window.Resources>
<sys:ViewModel x:Key="DataSource"/>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource DataSource}, Path=Persons}"
DisplayMemberPath="Name"
HorizontalAlignment="Left" Margin="49,21,0,201" Name="listBox1" Width="425" />
<Grid DataContext="{Binding Source={StaticResource DataSource}}" Height="149" HorizontalAlignment="Left" Margin="49,134,0,0" Name="grid1" VerticalAlignment="Top" Width="425">
<DataGrid
ItemsSource="{Binding Source={StaticResource DataSource}, Path=Persons}"
AutoGenerateColumns="False" Height="143" HorizontalAlignment="Left" Margin="36,0,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="383" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Age}" Header="Age" ></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="296,290,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.Navigation;
using System.Windows.Shapes;
namespace bild
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ViewModel r = new ViewModel();
r.ad();
dataGrid1.ItemsSource = r.Persons;
}
}
public class ViewModel
{
public ViewModel()
{
this.Persons = new ObservableCollection<Person>();
this.Persons.Add(new Person("Ivan", 23));
this.Persons.Add(new Person("Stefan", 34));
this.Persons.Add(new Person("Maria", 16));
this.Persons.Add(new Person("Michael", 78));
}
public void ad()
{
this.Persons = new ObservableCollection<Person>();
this.Persons.Add(new Person("Maria", 16));
this.Persons.Add(new Person("Michael", 78));
}
public ObservableCollection<Person> Persons
{
get;
set;
}
}
public class Person
{
public Person(string name, int age)
{
this.Name = name;
this.Age = age;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
}
|
|
|
|
|
What is the exact problem?
After a quick view, I see two things:
1)
After calling the ctor of ViewModel, there will be Ivan, Stefan, Maria and Michael in the Persons collection. After calling ViewModel.Add() there will be only Maria and Michael, because you create a new collection.
2)
class Person needs to implement INotifyPropertyChanged and after changing a property it has to call the PropertyChangedEvent.
Hope it helps
Andy
|
|
|
|
|
Good Day All
i am building Kinect Application, obviously using WPF.. Now i have a problem with Navigation. I have a Window and others are Pages that will use a Frame in a Window. So the Window has Button to Main to the "Menu" so the Page that contains Menu's is called "MainMenu.xaml"
private void btnEnter_Click(object sender, RoutedEventArgs e)
{
_mainFrame.Source = new Uri("/MainMenu.xaml", UriKind.Relative);
}
now when i navigate to the Menu Page , it shows my Menu page ,but the Window Button is not hidden.
So Before i used to have the code to navigate to the "MainMenu.xaml" in the loaded event of the Window so that it directly moved to the Menu, but my Problem is that still behind even if its behind my Kinect Gesture can click or touch the menu's in the "MainMenu.xaml" it looks like they are not out of scope.
When i move from **MainMenu.xaml** to the selected menu page i do it this way
(Application.Current.MainWindow.FindName("_mainFrame") as Frame).Source = new Uri("/Alphabets101/Menu.xaml", UriKind.Relative);
but still you will see that it tries to Access a "MainMenu" even if they are not visible on the screen
Thanks
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa[at]dotnetfunda.com
http://www.Dotnetfunda.com
|
|
|
|
|
Dear All, Good day.
Need your help on threading.
I want to use a wpf window as progress window across my project.
On the main UI thread while doing the business logics i need to open a new wpf window in separate thread as progress window and once the Business logic is done i want to close the same.
I googled and found couple of examples to open a wpf window in separate thread and with new STA.. but am not able to find how to close the same window once done.
My progress window doesn't allow the user to close it.
global variable
progresswindow _pw = new progresswindow();
ShowProgress()
{
_pw.Show();
}
HideProgress()
{
_pw.Hide();
}
MainThreadBusinessLogic()
{
LogicStarts...
ShowProgress();
LogicEnds..
HideProgress();
}
This is wat i wanted to do. But as the main thread is very busy it wont show the progress window i know. I used the background worker and showed the progress bar.. But the thing is i have different business logic which will work based on the users clicks.. so i may need to create different workers for handling those.
If i do something like above mentioned window as progress window i can commonly use the same for all places.
Hence request your help here my friends.
thanks in advance.
|
|
|
|
|
You are doing it all backwards.
All UI lives on the main thread (including the progress window)
All business logic lives on a background thread
When you need to update the progress dialog from the background thread, use Dispatcher.Invoke.
|
|
|
|
|
Hi Sledge,
Thanks for your reply.
And you are right i am doing in the back ward only.
But the thing is, I have more the 20 forms in my application which have different functionality. And even in single form i have more than 5 buttons which will do different business logic's. So that is reason i wanted to do this approach, other wise i need to create separate background workers for all buttons or single background worker with different parameters.. which is more messy.
So i planned to have a separate progress window which can be accessed across all the forms.
But as we know both are in same thread causing issue. i planned to open and use the new window in different thread.
Expecting your reply thanks in advance.
|
|
|
|
|
Hema Bairavan wrote: which is more messy
LOL... no, it's not. The way you have it now won't even work. The main UI will be completely unresponsive, have painting issues, etc.
You can use async functions, lamba expressions, etc.
Bottom line is, you'll have to move your business logic to the background thread in order to make your app work.
|
|
|
|
|
Try looking at the BackgroundWorker class. It provides an encapsulation for a worker thread that supports passing updates to the UI taking care of all the dirty cross-thread marshaling. It is very easy to use and supports aborting as well.
As for your progress window, you need to raise it when the first background thread is started and close it when the last background thread ends. You may want to consider creating the form at app startup then setting all of the thread progress notifications to call a notify update method on the form. The form could then determine if it needs to show/hide and which thread(s) progress to show. Again, if you use a backgroundworker, there is a thread complete event which fires when the thread is done making it easy to do something "on the way out the door". The progress window should be event-driven... only responding to events raised by the worker threads.
|
|
|
|
|
Hi All,
My actual probelm is related to user control view open in center of main parent window according to uploaded image.
Such as "WindowStartupLocation" property of window for window open in center location.But i want to user control view open in center of main parent window.
Thanks
|
|
|
|
|
hello ,
i want to ask about how can i rearrange elements inside wpf , i want to be able to move ( exp groupbox) and switch it's place with other groupbox and etc ...
exp: the bitdefender antivirus form .
M!dox
|
|
|
|
|
You can look at the Blacklight dock panels[^] to check if they give you the layout you are looking for.
|
|
|
|
|
Hi! Began studying wpf. Versed with custom control'ami. I create the control (simple button) from Control.
The question is - is it possible to not draw method OnRend (such as they call it), and make the template, the file generic.xaml? And still do not understand why does not my click handler.
файл MyButton.cs
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.Navigation;
using System.Windows.Shapes;
using System.Globalization;
namespace My_Lib
{
public class MyButton : Control
{
public static readonly RoutedEvent MyClickEvent;
static MyButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
MyClickEvent = EventManager.RegisterRoutedEvent("MyClick", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(MyButton));
}
public event RoutedEventHandler MyClick
{
add {
base.AddHandler(MyButton.MyClickEvent, value);
}
remove {
base.RemoveHandler(MyButton.MyClickEvent, value);
}
}
}
}
файл generic.xaml
xmlns:local="clr-namespace:My_Lib">
<LinearGradientBrush x:Key="Gray"
StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="PaleTurquoise" Offset="0" />
<GradientStop Color="#CCCCFF" Offset="0.5" />
<GradientStop Color="Teal" Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="Gray2"
StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="PaleGreen" Offset="0" />
<GradientStop Color="#99CC99" Offset="0.5" />
<GradientStop Color="Green" Offset="1" />
</LinearGradientBrush>
<Style TargetType="{x:Type local:MyButton}">
<Setter Property="Width" Value="100"></Setter>
<Setter Property="Height" Value="40"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyButton}">
<Border
x:Name="ButtonBorder"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
BorderBrush="White"
CornerRadius="25"
BorderThickness="2"
Background="{StaticResource Gray}"
Opacity="0.9"
>
<ContentPresenter x:Name="ButtonContentPresenter"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"
>
</ContentPresenter>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonBorder"
Property="Background"
Value="{StaticResource Gray2}">
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
файл MainWindow.xaml.cs
namespace MyComponentsTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void myButton1_MyClick(object sender, RoutedEventArgs e)
{
MessageBox.Show("sd");
}
}
}
|
|
|
|
|
We have an application portal where a user can logon and launch any of our applications they are authorised for. Currently this is written in ASPX launching Silverlight applications.
We now need to extend this to launch Excel workbooks but also to integrate the launcher with a control application. I would like to do this in Silverlight as that is where our skill set is and I REALLY like the browser deployment method. Silverlight out of browser may be an option (is it still available in SL5) but does that then require a different deployment method.
We have the xap and file locations and we assume the user has access to the network drive the Excel workbook is stored.
The question is whether it is possible for a user to launch Silverlight apps and Excel workbooks from within a Silverlight app.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
Hi.
I have a class "Files" that creates txt files, i want the progress bar and the multiline textbox in the xaml shows the process when its occurs.
I receive all the paramates at the MainWindows.cs and i see the progressbar1.value growing and the textbox1.text having the desired text but in the gui nothing happends.
WHY?
|
|
|
|
|
It's a semicolon at line 34 that's causing a problem.
You may wonder why I said that. Well, I said it because I can't see your code. How on earth would you expect me to be able to figure it out.
I would suspect that the issue you are facing is that you are doing all your processing on the primary thread, which will block the update of the UI until it's finished processing.
|
|
|
|
|
i really wondered!!!
here is the class that create the files:
class Farmonicior
{
private string sourcpath = string.Empty;
public string SourcePath
{
get
{
if (sourcpath==string.Empty)
global::System.Windows.MessageBox.Show("The Source Cannot be Empty!!!");
return sourcpath;
}
set {sourcpath = value;}
}
private string resultFolder = string.Empty;
public string ResultFolder
{
get
{
if ( resultFolder == string.Empty)
{
FileInfo fi = new FileInfo(sourcpath);
resultFolder = fi.DirectoryName;
}
return resultFolder;
}
set { resultFolder = value; }
}
public void FilesWork()
{
string line = string.Empty;
List<string> LinesList = new List<string>();
try
{
StreamReader StrRdr = new StreamReader(SourcePath);
while ((line = StrRdr.ReadLine()) != null )
{
if (line.StartsWith("LN"))
{
LinesList.Add(line);
}
}
StrRdr.Close();
}
catch (Exception ErrExc)
{
System.Windows.MessageBox.Show(ErrExc.Message,"Read Source.");
}
Singleton.Instance.ProgressBarMAX = LinesList.Count;
try
{
Singleton.Instance.ProgressBarVAL = 0;
string year = DateTime.Today.ToString("yy");
foreach (string item in LinesList)
{
string[] lineArr = item.Split('|');
string newfile = string.Format(@"{0}\{1}_{2}.txt", ResultFolder, lineArr[2], lineArr[3]);
Singleton.Instance.NewFilesPath.Add(newfile);
StreamWriter StrWtr = new StreamWriter(newfile, false);
StrWtr.WriteLine(string.Format("yy"));
StrWtr.WriteLine(string.Format("nn|tttttt"));
if (lineArr.Length == 8)
{
if (lineArr[7] != "")
StrWtr.WriteLine("qwertyui");
else
StrWtr.WriteLine("poiuytrewq");
}
else
StrWtr.WriteLine(string.Format("HR|{0}|{1}", lineArr[3], year));
StrWtr.WriteLine(string.Format("RTG|{0}|{1}", lineArr[5], lineArr[6]));
StrWtr.WriteLine("END");
StrWtr.Close();
Singleton.Instance.ProgressBarVAL++;
}
}
catch (Exception ErrExc)
{
System.Windows.MessageBox.Show(ErrExc.Message,"Create Files.");
}
}
}
Here the MainWindows.cs
public partial class MainWindow : Window,INotifyPropertyChanged
{
Farmonicior hrm = new Farmonicior();
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
PrgrssBar1.Value = 0;
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
dlg.Filter = "Text documents (.txt)|*.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
SourceFileTxtBx.Text = filename;
hrm.SourcePath = filename;
ResulFolderTxtBx.Text = hrm.ResultFolder;
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
string selectedFolder = string.Empty;
FolderBrowserDialog selectFolderDialog = new FolderBrowserDialog();
selectFolderDialog.ShowNewFolderButton = true;
if (selectFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Singleton.Instance.NewFilesPath.Clear();
FilesNamesTxtBx.Clear();
Singleton.Instance.ProgressBarVAL = 0;
selectedFolder = selectFolderDialog.SelectedPath;
ResulFolderTxtBx.Text = selectedFolder;
hrm.ResultFolder = selectedFolder;
}
}
private void GoBttn_Click(object sender, RoutedEventArgs e)
{
PrgrssBar1.Maximum = Singleton.Instance.ProgressBarMAX;
if (hrm.SourcePath == string.Empty)
System.Windows.MessageBox.Show("Must Select a source file");
else
{
foreach (string item in Singleton.Instance.NewFilesPath)
{
FilesNamesTxtBx.AppendText(item + Environment.NewLine);
}
FilesWork();
}
}
public void ProgressBarStep()
{
PrgrssBar1.Value++;
}
internal void updateProgressbarMax()
{
PrgrssBar1.Maximum = Singleton.Instance.ProgressBarMAX;
progresBarTxtBx.Text = "Total files: " + Singleton.Instance.ProgressBarMAX.ToString();
}
}
here the singleton
public sealed class Singleton
{
MainWindow MW = new MainWindow();
static readonly Singleton _instance = new Singleton();
public static Singleton Instance
{
get { return _instance; }
}
int progBarVal;
public int ProgressBarVAL
{
get { return progBarVal; }
set
{
progBarVal = value;
MW.ProgressBarStep();
}
}
int progBarMax;
public int ProgressBarMAX
{
get { return progBarMax; }
set
{
progBarMax = value;
MW.updateProgressbarMax();
}
}
List<string> newfilePath = new List<string>();
public List<string> NewFilesPath
{
get { return newfilePath; }
set
{
newfilePath = value;
}
}
private Singleton()
{
}
}
this is. I hope i copy enough. be nice if you see coding horrors. thanks.
|
|
|
|