|
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.
|
|
|
|
|
As Pete said, you're doing all your processing on the primary thread, which will block the update of the UI until it's finished processing.
Have a look at the BackgroundWorker[^] component - it makes it easier to do some work on a background thread and show the progress in the UI.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi.
Thanks for reply.
If i copy the FilesWork function to the MainWindows Class everything works OK. this still means i need the BackgroundWorker?.
modified 3-Feb-13 1:42am.
|
|
|
|
|
Thanks Richard.
The BackgroundWorker works and GUI works also. I forgot the ReportProgress so takes me some time to implemente it.
|
|
|
|
|
Hello, I've managed to setup an example working for both RoutedEvent and AttachedEvent but it appears the difference between the two is just syntactical as "RoutedEvent" handler can be declared in XAML in same syntax as "AttachedEvent". (I understand the difference between AttachedProperty vs DependencyProperty clearly)
Because of formatting issue with XAML - detail here:
http://forums.codeguru.com/showthread.php?534049-RoutedEvent-vs-AttachedEvent&p=2103409#post2103409[^]
Any idea anyone? Thanks
dev
|
|
|
|
|
Please don't raise the same question in multiple locations. You've just made it harder for yourself to get a cohesive answer. I'm not going to create an account on CodeGuru just to answer this question when the forums here are perfectly capable.
On the surface, it would appear that the two items are the same because they have the same syntax. Well, this is true to the extent that an attached event IS a routed event. A routed event, however, is not an attached event. In other words, an attached event is a specialised form of routed event.
So, what is an attached event? Well, basically, it's an event that can be attached to another element without the other element having to be aware that this capability is present. If you think about it as being like an attached property for events then it starts to make sense. So, why do we have this? Well, it allows you to extend WPF through composition rather than inheritance.
|
|
|
|
|
Pete O'Hanlon wrote:
So, what is an attached event? Well, basically, it's an event that can be attached to another element without the other element having to be aware that this capability is present. If you think about it as being like an attached property for events then it starts to make sense. So, why do we have this? Well, it allows you to extend WPF through composition rather than inheritance.
I understand perfect "Grid.Row" is an "Attached Property" that it's not a property of "TextBox" itself for instance. I understand analogy when this concept extended to "Events". However - what's confusing is the SYNTAX: you can define an event handler to a RoutedEvent the same way/syntax as you would with AttachedEvent.
But if you see the sample I gave in MainWindow.xaml, I can subscribe to "AttachedEvent" and "RoutedEvent" from xaml in same syntax - the syntax which does not require "Instance" of "GrandChild" (i.e. "MainWindow" does NOT implement event GrandChildRoutedEvClick itself, which is declared in "GrandChild")
From MainWindow.xaml, "RoutedEvent" handler setup like this:
<br />
local:GrandChild.GrandChildRoutedEvClick="GrandChild_GrandChildRoutedEvClick"<br />
From MainWindow.xaml, "AttachedEvent" handler setup like this:
<br />
local:GrandChild.GrandChildAttachedEvClick="GrandChildAttachedEvClickHandler" <br />
Despite in "MiddleChild.xaml", I setup handler to "RoutedEvent" the "Propery way"... what's real diff between Routed/AttachedEvent besides phylosophical or syntactical ...?
<br />
[local:GrandChild x:Name="Junior" VerticalAlignment="Stretch" GrandChildRoutedEvClick="GrandChild_GrandChildRoutedEvClick"/]<br />
What I am saying is, you can define a DockStateChanged event with syntax of a "RoutedEvent" - not "AttachedEvent", and in some parent control couple layers up, setup event handler using syntax as you would consuming an "AttachedEvent"
dev
modified 30-Jan-13 5:11am.
|
|
|
|
|
As I said. An attached event is a specialist form of routed event. I don't know how I can say it any clearer - other than an attached event IS A routed event that is used for a specific purpose that routed events aren't designed for.
|
|
|
|
|
|
Don't mention it. I hope it's clearer now.
|
|
|
|
|
I heard ya, I was looking for some sort of confirmation.
dev
|
|
|
|
|
I created a tab stle so that it has a close button. The button is bound to a command:
<Style TargetType="{x:Type TabItem}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Close"
Command="{Binding CloseTabCommand}"/>
<MenuItem Header="Close All But This"
Command="{Binding CloseAllOtherTabsCommand}"/>
<MenuItem Header="Float"
Command="{Binding FloatTabCommand}"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
I tried this:
<MenuItem Header="Close"
Command="{Binding CloseTabCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}"/>
but I get null passed to the command.
I need to pass along a reference to the tab. Can someone show me how to get the tab in this style and pass it as a parameter?
Thank you
If it's not broken, fix it until it is
modified 30-Jan-13 0:12am.
|
|
|
|
|
You won't find the TabItem like that because the ContextMenu is a pop up window and thus is in a different visual tree.
Do this instead:
{Binding Path=PlacementTarget, RelativeSource={RelativeSource
AncestorType=ContextMenu}}
|
|
|
|
|
Ok, I finally got around to this again. So now I'm trying this:
<Style TargetType="{x:Type TabItem}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Close"
Command="{Binding CloseTabCommand}"
CommandParameter="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</ContextMenu>
</Setter.Value>
</Setter>
Here's my command:
private ICommand _CloseTabCommand;
public ICommand CloseTabCommand
{
get
{
if (_CloseTabCommand == null)
_CloseTabCommand = new RelayCommand<object>(p => closeTabExecuted(p), p => closeTabCanExecute(p));
return _CloseTabCommand;
}
}
and the methods:
private bool closeTabCanExecute(object obj)
{
return true;
}
private void closeTabExecuted(object obj)
{
TabManager.CloseActiveTab();
}
Again, the command fires but null is passed to closeTabExecuted.
I'm stuck here. Could use some more help.
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Works all day long for me. I copy & pasted your code and got the TabItem in both closeTabExecuted and closeTabCanExecute. Are you sure your RelayCommandT is not broken?
|
|
|
|
|
I am new to WPF applications. I come from a VB6 Windows Forms background. I used to create user controls that I used in UI's to create graphic content controls like buttons with normal, down,and hover images. While I know the ultimate way to do this is create a separate project for my custom controls I am just working withing a single project to get things worked out first.
I have created a Controls folder where I have added several custom controls each inherting from a base control like Button, RadioButton, Slider, Checkbox etc. I can then add properties to each custom control for DefaultImage, DownImage, HoverImage and in the case of a Slider GripImage and GutterImage. The properties have a public set/get property so I can set the images for a specific control and then the control behavior can set the proper image.
There is an Images folder where I put all the images needed for the application.
I have created a Images.xaml ResourceDictionary that defines key names like ButtonDown to the specific image names so the image naming in the template is easier and I can swap out images easily.
A Generic.xaml gets created in the Themes folder where the basic Control Template goes. Not sure if this is best practice for storing each Control Template or not but it is what gets created by default.
My main area of difficulty is creating the template. I know what I want to do but can't get the syntax to work correctly.
In the case of the custom button I want to create triggers for IsPressed and IsMouseOver and set the controls Background ImageBrush to the proper image. When those triggers are false I want to set the background image to the DefaultImage
I'm not sure about the syntax for refering to the image property of the custom Control and how best to access it in the xamal.
I've looked at a lot of examples but none seem to do things the way I am organizing it. I don't want to hard code the images to the control. My ultimate goal is to be able to drag the custom control into a grid using the designer and set the Default, Down, and Hover image properties and then place the control how I want it in the designer. Then if I want to change the entire look of the app I can just swap out images in the Images folder and the GUI will change.
|
|
|
|
|
For starters, you should only use images when its actually an image. For other cases, you should draw the part using XAML primitives. In the example you mentioned, the slider thumb and gutter and tick marks, etc. should all be drawn in XAML primitives. You'll find out why down the road .
When you are inheriting from a system control, you should generally NOT write your own template from scratch, but rather dump out the Microsoft template and either inherit from it, or paste it into your assembly and modify it. Reason being is that while Microsoft pushes "lookless" controls (the XAML has no knowledge of the code and the code has no knowledge of the XAML), you'll find out they are not. Controls often rely on primitives being in certain layouts and having certain names.
Dumping out the Microsoft templates is also a GREAT way to teach you how real world controls are put together.
I typically use Expression Blend for dumping out templates.
NOTE: unless you are going to have a control that is themeless, you need to do a lot of tricks to have say an XP theme, an Aero theme, Green theme, Blue Theme,etc. so you respect the users settings. If you are just going to have one look & feel across all themes / OS'es, etc. then you don't need to worry about that as much.
Just follow the Microsoft template lead to be honest with you... they are doing it "the right way".
|
|
|
|
|
I should probably point out that the UI's that I build are VERY graphic in nature with borderless nonsizeable main windows with irregular shaped graphic backgrounds. All Buttons, radios, checkboxes, sliders and other basic controls are designed by a graphic designer and then sliced up for the GUI. These are NOT standard UI's. The GUI's are used for small utility setting setup (think tooltray app for setting up some small feature) or they are internal tools that are used by non computer people or used as a demo on a large screen or demo room with specific hardware to demonstrate a feature.
That said...how do I "Dump out" a control template?
|
|
|
|