WPF Page navigation - wpf

I use a custom textblock in my WPF Application, when I use it in WPF Windows it worked good but when I use it in a WPF Page it make a problem. When I click on a link in my Custom Control it browse the link and show in browser but the WPF page navigate back to another WPF Page too (first page)
namespace Dtwitter.Controls
{
public class TweetTextBlock : TextBlock
{
public TweetTextBlock()
{
}
#region Dependency properties
public string TweetText
{
get { return (string)GetValue(TweetTextProperty); }
set { SetValue(TweetTextProperty, value); }
}
// Using a DependencyProperty as the backing store for TweetText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TweetTextProperty =
DependencyProperty.Register("TweetText", typeof(string), typeof(TweetTextBlock),
new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(OnTweetTextChanged)));
#endregion
private static void OnTweetTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
string text = args.NewValue as string;
if (!string.IsNullOrEmpty(text))
{
TweetTextBlock textblock = (TweetTextBlock)obj;
textblock.Inlines.Clear();
textblock.Inlines.Add(" ");
string[] words = Regex.Split(text, #"([ \(\)\{\}\[\]])");
string possibleUserName = words[0].ToString();
if ((possibleUserName.Length > 1) && (possibleUserName.Substring(1, 1) == "#"))
{
textblock = FormatName(textblock, possibleUserName);
words.SetValue("", 0);
}
foreach (string word in words)
{
// clickable hyperlinks
if (UrlShorteningService.IsUrl(word))
{
try
{
Hyperlink link = new Hyperlink();
link.NavigateUri = new Uri(word);
link.Inlines.Add(word);
link.Click += new RoutedEventHandler(link_Click);
link.ToolTip = "Open link in the default browser";
textblock.Inlines.Add(link);
}
catch
{
//TODO:What are we catching here? Why? Log it?
textblock.Inlines.Add(word);
}
}
// clickable #name
else if (word.StartsWith("#"))
{
textblock = FormatName(textblock, word);
}
// clickable #hashtag
else if (word.StartsWith("#"))
{
string hashtag = String.Empty;
Match foundHashtag = Regex.Match(word, #"#(\w+)(?<suffix>.*)");
if (foundHashtag.Success)
{
hashtag = foundHashtag.Groups[1].Captures[0].Value;
Hyperlink tag = new Hyperlink();
tag.Inlines.Add(hashtag);
string hashtagUrl = "http://search.twitter.com/search?q=%23{0}";
// The main application has access to the Settings class, where a
// user-defined hashtagUrl can be stored. This hardcoded one that
// is used to set the NavigateUri is just a default behavior that
// will be used if the click event is not handled for some reason.
tag.NavigateUri = new Uri(String.Format(hashtagUrl, hashtag));
tag.ToolTip = "Show statuses that include this hashtag";
tag.Tag = hashtag;
tag.Click += new RoutedEventHandler(hashtag_Click);
textblock.Inlines.Add("#");
textblock.Inlines.Add(tag);
textblock.Inlines.Add(foundHashtag.Groups["suffix"].Captures[0].Value);
}
}
else
{
textblock.Inlines.Add(word);
}
}
textblock.Inlines.Add(" ");
}
}
public static TweetTextBlock FormatName(TweetTextBlock textblock, string word)
{
string userName = String.Empty;
string firstLetter = word.Substring(0, 1);
Match foundUsername = Regex.Match(word, #"#(\w+)(?<suffix>.*)");
if (foundUsername.Success)
{
userName = foundUsername.Groups[1].Captures[0].Value;
Hyperlink name = new Hyperlink();
name.Inlines.Add(userName);
name.NavigateUri = new Uri("http://twitter.com/" + userName);
name.ToolTip = "View #" + userName + "'s recent tweets";
name.Tag = userName;
name.Click += new RoutedEventHandler(name_Click);
if (firstLetter != "#")
textblock.Inlines.Add(firstLetter);
textblock.Inlines.Add("#");
textblock.Inlines.Add(name);
textblock.Inlines.Add(foundUsername.Groups["suffix"].Captures[0].Value);
}
return textblock;
}
static void link_Click(object sender, RoutedEventArgs e)
{
try
{
System.Diagnostics.Process.Start(((Hyperlink)sender).NavigateUri.ToString());
}
catch
{
//TODO: Log specific URL that caused error
MessageBox.Show("There was a problem launching the specified URL.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
}
}

change your link click method to
static void link_click(Object sender, RequestNavigateEventArgs e) {
try {
System.Diagnostics.Process.Start(e.Uri.ToString());
} catch {
//TODO: Log specific URL that caused error
MessageBox.Show("There was a problem launching the specified URL.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
} finally {
e.Handled = true;
}
}
change your
link.Click+=new RoutedEventHandler(link_Click);
to
link.RequestNavigate+=new RequestNavigateEventHandler(link_Click);
Set e.Handled=true in link_click to mark you've dealt with the link click to prevent the framework from additionally processing your link click further.
Alternatively you may be able to just set the TargetName property of Hyperlink to "_blank" and not need the process start command

The code below should make it work the same way in both cases (Page and Window)....
try this to open the hyperlink in web browser in MouseDown of the Hyperlink object.
Process.Start((e.OriginalSource as Hyperlink).NavigateUri.ToString());
e.Handled = true;
Let me know if this helps.

Related

How to disable remove button for KryptonPage with context menu close item?

How to block KryptonPage for close and i need to remove button of docking area and context menu option for close
i am using https://github.com/ComponentFactory/Krypton
Visual Studio 2015 Community
Win 10
There are details about my code for clear understanding
Following screenshot is Form designer about using controls
screenshot of Form designer using controls 1
screenshot of Form designer using controls 2
Following screenshot of ruining application
application ruining time for close menu item for block i want it
There are my form code c#
// this is main form code
public partial class frmMain : KryptonForm
{
public frmMain()
{
InitializeComponent();
kryptonPanel1.Dock = DockStyle.Fill;
kryptonDockableWorkspace.Dock = DockStyle.Fill;
kryptonPanel.Dock = DockStyle.Fill;
}
private void Form1_Load(object sender, EventArgs e)
{
KryptonDockingWorkspace w = kryptonDockingManager.ManageWorkspace(kryptonDockableWorkspace);
kryptonDockingManager.ManageControl(kryptonPanel, w);
kryptonDockingManager.ManageFloating(this);
kryptonDockingManager.AddDockspace("Control", DockingEdge.Left, new KryptonPage[] { NewWinTools() });
kryptonDockingManager.AddDockspace("Control", DockingEdge.Bottom, new KryptonPage[] { NewWinTools(), NewWinTools(), NewWinTools(), NewWinTools() , NewWinTools() });
kryptonDockingManager.AddToWorkspace("Workspace", new KryptonPage[] { NewWinControl() });
}
private KryptonPage NewPage(string name, int image, Control content)
{
// Create new page with title and image
KryptonPage p = new KryptonPage();
p.Text = name;
p.TextTitle = name;
p.TextDescription = name;
p.ImageSmall = imageListSmall.Images[image];
// Add the control for display inside the page
content.Dock = DockStyle.Fill;
p.Controls.Add(content);
return p;
}
private KryptonPage NewWinControl()
{
UserControl1 br = new UserControl1();
KryptonPage page = NewPage("New Tab", 0, br);
page.ClearFlags(KryptonPageFlags.DockingAllowAutoHidden | KryptonPageFlags.DockingAllowDocked);
return page;
}
private KryptonPage NewWinTools()
{
UserControl1 br = new UserControl1();
KryptonPage page = NewPage("Tools 1 ", 2, br);
page.ClearFlags( KryptonPageFlags.DockingAllowClose);
return page;
}
}
please help for block close KryptonPage
I have just come across the same issue. I can see that this question was answered here
It looks like there is no way of actually removing the close button but you can intercept it and then ignore the event.
In the interest of completeness, this is the code you need to write:
private void CreateDockWorkspace()
{
KryptonDockingWorkspace w = kryptonDockingManager.ManageWorkspace("Workspace", kryptonDockableWorkspace);
kryptonDockingManager.ManageControl("Control", kryptonPanel, w);
kryptonDockingManager.ManageFloating("Floating", this);
kryptonDockingManager.AddToWorkspace("Workspace", new KryptonPage[] {
NewPage("Overview"),
NewPage("Main"),
NewPage("Report"),
});
// This is where you wire up the CloseAction event handler
var workspace = kryptonDockingManager.CellsWorkspace.FirstOrDefault();
workspace.CloseAction += HandleTabCloseAction;
}
private void HandleTabCloseAction(object sender, CloseActionEventArgs e)
{
// This event handler ignores the action
e.Action = CloseButtonAction.None;
}
private KryptonPage NewPage(string name)
{
var p = new KryptonPage();
p.Text = name;
p.TextTitle = name;
p.TextDescription = name;
content.Dock = DockStyle.Fill;
p.Controls.Add(content);
return p;
}

datagridview control continues update, gui flickers when scrolling window

Problem:
I have a Winform application with a form en on this form i have a databound DataGridView.
The datagridview is updated from the backend by updating the bind object continuesly using a timer to get the data every 10 seconds. In order to update the gui with this new data i call a RefreshDatabindings. (if i do not do this, the gui is nog updated, i am binding to a BindingList and the object implement the INotifyPropertyChanged)
When the form is big enough to show the whole datagridview at once everything is working wel. But when the form is not big enough to show the hole datagridview a scrollbar appears.
When i scroll to the right to see the rest of the datagridview i see the gui flickering (only the part that wasn't visible before scrolling). When i strech the form to make de gridview fitting again, everything is working wel (no flashing and flickering). the flickering only happens when i have to scroll.
I am lost, can please somebody help me :)?
I allready tryed the DoubleBuffered = true.
Thanks in advance!
BindingList<InstanceTableViewModel> viewModelList;
public Form1()
{
InitializeComponent();
DoubleBuffered = true;
functionParamList = new List<FunctionParameter>();
functionParamList.Add(new FunctionParameter { DeviceValue = 100, InstanceId = "1", Name = "A" });
functionParamList.Add(new FunctionParameter { DeviceValue = 200, InstanceId = "2", Name = "B" });
functionParamList.Add(new FunctionParameter { DeviceValue = 300, InstanceId = "3", Name = "C" });
viewModelList = CreateInstanceTableViewModelList();
dataGridView1.DataSource = viewModelList;
//Create timer
updateDataTimer = new System.Timers.Timer();
updateDataTimer.Interval = 500;
updateDataTimer.Elapsed += updateDataTimer_Elapsed;
updateDataTimer.Start();
}
private void updateDataTimer_Elapsed(object sender, ElapsedEventArgs e)
{
ThreadPool.QueueUserWorkItem(ReadDataThreadPoolMethod);
}
private void ReadDataThreadPoolMethod(object state)
{
Random random = new Random();
int randomNumber = random.Next(0, 100);
foreach (FunctionParameter param in functionParamList)
{
param.DeviceValue = Convert.ToInt64(randomNumber);
}
}
void functionParameter_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var functionParameter = sender as FunctionParameter;
var propertyName = e.PropertyName;
var propertyValue = functionParameter.DeviceValue;
var parameterName = functionParameter.Name;
UpdateViewModel(functionParameter.InstanceId, propertyName, propertyValue, parameterName);
}
private void UpdateViewModel(string instanceId, string propertyName, long propertyValue, string parameterName)
{
var instanceViewModel = viewModelList.Single(x => x.InstanceId == instanceId && x.NameLabel == parameterName);
if (instanceViewModel != null)
{
instanceViewModel.ValueHex = Convert.ToUInt16(propertyValue);
}
ResetBindingsSource();
}
delegate void UpdateBindingsInvoker();
public void ResetBindingsSource()
{
if (!this.IsDisposed)
{
if (this.InvokeRequired)
{
this.Invoke(new UpdateBindingsInvoker(UpdateDataGrid));
}
else
{
UpdateDataGrid();
}
}
}
private void UpdateDataGrid()
{
dataGridView1.Refresh();
}
So here my solution:
You only uses the Forms DoubleBuffering, but the following code is an extension method to the DataGridview and successfully works (at my tests ;)
public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}
I found this code right here at Codeprojct.
You can use it in this way:
YourDataGridView.DoubleBuffered(true);
I hope i could help you ^^

Change DataPager text

This might sound like a strange request and i'm not sure if it's actually possible, but I have a Silverlight DataPager control where it says "Page 1 of X" and I want to change the "Page" text to say something different.
Can this be done?
In DataPager style there is a part by name CurrentPagePrefixTextBlock by default its value is "Page".
You can refer http://msdn.microsoft.com/en-us/library/dd894495(v=vs.95).aspx for more info.
One of the solution is to extend DataPager
Here is the code to do that
public class CustomDataPager:DataPager
{
public static readonly DependencyProperty NewTextProperty = DependencyProperty.Register(
"NewText",
typeof(string),
typeof(CustomDataPager),
new PropertyMetadata(OnNewTextPropertyChanged));
private static void OnNewTextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var newValue = (string)e.NewValue;
if ((sender as CustomDataPager).CustomCurrentPagePrefixTextBlock != null)
{
(sender as CustomDataPager).CustomCurrentPagePrefixTextBlock.Text = newValue;
}
}
public string NewText
{
get { return (string)GetValue(NewTextProperty); }
set { SetValue(NewTextProperty, value); }
}
private TextBlock _customCurrentPagePrefixTextBlock;
internal TextBlock CustomCurrentPagePrefixTextBlock
{
get
{
return _customCurrentPagePrefixTextBlock;
}
private set
{
_customCurrentPagePrefixTextBlock = value;
}
}
public CustomDataPager()
{
this.DefaultStyleKey = typeof(DataPager);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
CustomCurrentPagePrefixTextBlock = GetTemplateChild("CurrentPagePrefixTextBlock") as TextBlock;
if (NewText != null)
{
CustomCurrentPagePrefixTextBlock.Text = NewText;
}
}
}
Now by setting NewText property in this CustomDataPager we can get whatever text we want instead of "Page"
xmlns:local="clr-namespace:Assembly which contains CustomDataPager"
<local:CustomDataPager x:Name="dataPager1"
PageSize="5"
AutoEllipsis="True"
NumericButtonCount="3"
DisplayMode="PreviousNext"
IsTotalItemCountFixed="True" NewText="My Text" />
Now it displays "My Text" Instead of "Page".
But other parts also need to be customised inorder make this work correctly!!
Hope this answers your question

How to log name/path of clicked control in WPF?

I use the following code to log every click in our WinForms application. In essence it looks up a control from its HWND and then prints the types and names of the control and all of its parents. Something like MainForm"myWindow">TabPanel"mainTab">Button"save"
internal class ClickLogger : IMessageFilter
{
private const int WM_LBUTTONDOWN = 0x0201;
private const int WM_LBUTTONDBLCLK = 0x0203;
private const int WM_RBUTTONDOWN = 0x0204;
private const int MaxRecurseDepth = 30;
private readonly ILogger _log;
public ClickLogger(ILogger logger)
{
_log = logger;
}
[DebuggerStepThrough]
public bool PreFilterMessage(ref Message message)
{
if (message.Msg == WM_LBUTTONDOWN
|| message.Msg == WM_RBUTTONDOWN
|| message.Msg == WM_LBUTTONDBLCLK)
{
string path = "Unknown";
Control ctl = Control.FromHandle(message.HWnd);
if (ctl != null)
{
path = PathFromControl(ctl, MaxRecurseDepth).ToString();
}
string logEntry = string.Format("{0} Click on {1}",
WndMsgToClickName(message.Msg), path);
if (_log.IsInfoEnabled)
{
_log.Info(logEntry);
}
}
return false;
}
private StringBuilder PathFromControl(Control control, int maxDepth)
{
if(maxDepth == 0)
{
_log.Warn("Max recursion {0} reached whilst resolving path of control", MaxRecurseDepth);
return new StringBuilder("ERR");
}
string name = control.GetType().Name;
if (control.Name.IsNotBlank())
{
name = name + "\"" + control.Name + "\"";
}
if (control.Parent != null && control.Parent != control)
{
return PathFromControl(control.Parent, maxDepth - 1).Append(">").Append(name);
}
return new StringBuilder(name);
}
public void Initialize()
{
Application.AddMessageFilter(this);
}
private static string WndMsgToClickName(int msgId)
{
switch (msgId)
{
case WM_LBUTTONDOWN:
return "Left";
case WM_LBUTTONDBLCLK:
return "Double";
case WM_RBUTTONDOWN:
return "Right";
default:
return "0x" + Convert.ToString(msgId, 16);
}
}
}
Recently we've started to mix WPF and WinForms and the above click logger simply prints "Unknown" for any click on a WPF control.
Is there a way I can perform a similar trick for WPF controls? A method that would work across technologies would be great.
well, it doesn't exactly work across technologies but for wpf you can use a combination of this to get the clicks and any of the helpers in this question to cycle through the parents to get the path.

Winforms DataBind to Control's Visible Property

Are there any known issues when databinding to a control's visible property?
The control is always NOT visible regardless of what my property is.
Public ReadOnly Property IsRibbonCategory() As Boolean
Get
Return True
End Get
End Property
I tried the control's text property and other properties and they seem to work correctly.
I am trying to set a Panel's visible property.
I've found that life is better if you assume that binding to a control's Visible property is broken, despite the fact that it sometimes works. See http://support.microsoft.com/kb/327305, which says as much (and while the KB article applies to .NET 1.0 and 1.1, it still seems to be a problem in at least 2.0).
I created a utility class for creating bindings which, among other things, gave me a centralized place to add a work-around. Instead of actually creating a binding on Visible it does two things:
It subscribes to the data source's INotifyPropertyChanged.PropertyChanged event and sets the Visible value as appropriate when the event is raised.
It sets the initial value of Visible according to the current data source value.
This required a little reflection code, but wasn't too bad. It is critical that you don't bind the Visible property and do the work-around or it won't work.
Workaround: Set the Visible property on the BindingComplete event.
I had same issue setting a label's Visible property - always stays false, even though setting the Enabled property works fine.
I just hit this issue in .NET 4.7.1 and Visual Studio 2017. To fix it, I changed the Visible property on my control to be initially set to True, as I had it as False previously.
Things to check:
Be sure you've instantiated the class that has the IsRibbonCategory property
Did you set the datasource of property of the binding source to the instance of the class
The datasource update mode should be on "on validation"
Make sure you didn't set the visible property manually to false on the control
Hope that helps. Can you post more code?
A workaround would be to use a Component to databind to a control's visiblity property instead of directly binding to the control's visibility property.
See below code:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public class ControlVisibilityBinding : Component
{
private static readonly object EventControlChanged = new object();
private static readonly object EventVisibleChanged = new object();
private System.Windows.Forms.Control _control;
private bool _visible = true;
public event EventHandler VisibleChanged
{
add { Events.AddHandler(EventVisibleChanged, value); }
remove { Events.RemoveHandler(EventVisibleChanged, value); }
}
public event EventHandler ControlChanged
{
add { Events.AddHandler(EventControlChanged, value); }
remove { Events.RemoveHandler(EventControlChanged, value); }
}
public ControlVisibilityBinding()
{
}
public ControlVisibilityBinding(IContainer container)
{
container.Add(this);
}
[DefaultValue(null)]
public System.Windows.Forms.Control Control
{
get { return _control; }
set
{
if(_control == value)
{
return;
}
WireControl(_control, false);
_control = value;
if(_control != null)
{
_control.Visible = _visible;
}
WireControl(_control, true);
OnControlChanged(EventArgs.Empty);
OnVisibleChanged(EventArgs.Empty);
}
}
[DefaultValue(true)]
public bool Visible
{
get { return _visible; }
set
{
if(_visible != value)
{
_visible = value;
}
if(Control != null)
{
Control.Visible = _visible;
}
OnVisibleChanged(EventArgs.Empty);
}
}
private void WireControl(Control control, bool subscribe)
{
if(control == null)
{
return;
}
if(subscribe)
{
control.VisibleChanged += Control_VisibleChanged;
}
else
{
control.VisibleChanged -= Control_VisibleChanged;
}
}
private void Control_VisibleChanged(object sender, EventArgs e)
{
OnVisibleChanged(EventArgs.Empty);
}
protected virtual void OnVisibleChanged(EventArgs e)
{
EventHandler subscribers = (EventHandler)Events[EventVisibleChanged];
if(subscribers != null)
{
subscribers(this, e);
}
}
protected virtual void OnControlChanged(EventArgs e)
{
EventHandler subscribers = (EventHandler)Events[EventControlChanged];
if(subscribers != null)
{
subscribers(this, e);
}
}
}
static class Program
{
[STAThread]
static void Main()
{
using(Form form = new Form())
using(FlowLayoutPanel groupBoxLayoutPanel = new FlowLayoutPanel())
using(RadioButton visibleButton = new RadioButton())
using(RadioButton hiddenButton = new RadioButton())
using(GroupBox groupBox = new GroupBox())
using(Label text = new Label())
using(ControlVisibilityBinding visibilityBinding = new ControlVisibilityBinding())
using(TextBox inputTextBox = new TextBox())
{
groupBoxLayoutPanel.Dock = DockStyle.Fill;
groupBoxLayoutPanel.FlowDirection = FlowDirection.LeftToRight;
groupBoxLayoutPanel.AutoSize = true;
groupBoxLayoutPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
visibleButton.Text = "Show Label";
visibleButton.AutoSize = true;
hiddenButton.Text = "Hide Label";
hiddenButton.AutoSize = true;
groupBoxLayoutPanel.Controls.Add(visibleButton);
groupBoxLayoutPanel.Controls.Add(hiddenButton);
inputTextBox.Text = "Enter Label Text Here";
inputTextBox.Dock = DockStyle.Top;
groupBox.AutoSize = true;
groupBox.AutoSizeMode = AutoSizeMode.GrowAndShrink;
groupBox.Controls.Add(groupBoxLayoutPanel);
groupBox.Dock = DockStyle.Fill;
text.AutoSize = true;
text.ForeColor = Color.Red;
text.Dock = DockStyle.Bottom;
text.BorderStyle = BorderStyle.FixedSingle;
text.Font = new Font(text.Font.FontFamily, text.Font.Size * 1.25f, FontStyle.Bold | FontStyle.Italic);
text.DataBindings.Add("Text", inputTextBox, "Text", true, DataSourceUpdateMode.Never);
visibilityBinding.Control = text;
visibleButton.DataBindings.Add("Checked", visibilityBinding, "Visible", true, DataSourceUpdateMode.OnPropertyChanged);
Binding binding = hiddenButton.DataBindings.Add("Checked", visibilityBinding, "Visible", true, DataSourceUpdateMode.OnPropertyChanged);
ConvertEventHandler invertConverter = (sender, e) => e.Value = !((bool)e.Value);
binding.Format += invertConverter;
binding.Parse += invertConverter;
form.Controls.Add(inputTextBox);
form.Controls.Add(text);
form.Controls.Add(groupBox);
Application.Run(form);
}
}
}
}
Here is my turn around, it may be stupid but it worked many times.
I put one Panel control in my form, I make it to Fill my form and I put everything in that Panel. All the controls I bind the Visible property see their visibility change according to the objects in my DataGridView.

Resources