I'm trying to change the default style of the contextmenu to 2013/2015 in my rehosted vs13 application.
The problem occurs in only one designer, everywhere else its the correct one. I've tried to override both the XAML code and the code behind, checked if something else was changing the style, but without anykind of result.
Is there even a way to change the default style? Am I overseeing something?
Okay, after some heavy research/try and error if finally found out what was wrong: I couldnt access the control I wanted to change the ordinary way, so I had to think outside of the box (and ask a collegue for help).
This is the code that works for me, its not pretty, but it deletes the 'standard'-style set by WPF.
var dv = wd.Context.Services.GetService<DesignerView>();
dv.MenuItemStyle = null;
dv.MenuSeparatorStyle = null;
dv.Resources[typeof(ContextMenu)] = new Style(typeof(ContextMenu));
Quick thanks to Glen Thomas for trying to help.
Related
I need to refresh/update a ListView in my App. Since using INotifyPropertyChanged wouldn't be usefull in my case, is there any way to do this? I saw explanations for this in WPF but they do not work.
What i tried so far:
this.itemListView.Update();
this.itemListView.Items.Update();
this.itemListView.Refresh();
this.itemListView.Items.Refresh();
this.itemListView.UpdateLayout();
none of them worked.
also the final one did not work, even if it should be, because it exists.
im going through the same thing,
i tried to nullify the ItemsSource and assignig it again,
something like :
itemListView.ItemsSource = null;
itemListView.ItemsSource = group.Items;
The solution was to reset my ItemSource:
itemListView.ItemsSource = group.Items
I want to disable "bold" toggle button in an Excel sheet.
How can I do it?
I have the following code but it's not working:
CommandBarControl test = excel1.Application.CommandBars["Formatting"].FindControl(Id:113,Recursive:true);
if (test.Enabled)
{
MessageBox.Show(test.Caption + " enabled");
test.Visible = false;
test.Enabled = false;
}
I think it's not working because from Office 2007 they are using Ribbon Controls.
Can anyone help how to get the control of a specific button? So that I can change it's properties, enable/disable it by default, etc.
I'm afraid the answer is indeed that it isn't possible.
I've been looking at possibilities with class modules, because I thought that using a class, you could intercept the event that changes text to bold and then cancel that event. However, everything I could find was related to other events (value changes, calculation, workbook structure changes etc).
Even if it would work though, it would involve some serious coding and be error prone.
Maybe you're going at it the wrong way - what's the reason you remove this button? Probably there's another solution to your problem.
And as mentioned before, removing the button doesn't block the possibility to use ctrl+B or to paste bold text - you simply can't prevent this.
Not the answer you want, I'm sure, but I'm afraid this cannot be done.
I have a C# WPF application with a bunch of labels.
When I run my program it does some checks and wether it the check was positive or not it sets it's corrisponding label to green og red.
These changes is done in my .cs file like:
lblCheck14.Foreground = new SolidColorBrush(Colors.Green);
I would like to add a "Reset" button, that reset the application to it's initial start.
How can I easiest implement this?
One way - but I really hope there is a smarter way, is to set them all like:
lblCheck14.Foreground = new SolidColorBrush(Colors.Black);
lblCheck21.Foreground = new SolidColorBrush(Colors.Black);
lblCheck42.Foreground = new SolidColorBrush(Colors.Black);
Etc..
But isn't there a function which I can call that strips away any changes the .cs file have done to the controls in the XAML file? Like make the XAML back to stock?
Sorry for my back explanation. Hope you understand me :)
Best regards
Implement styles. You can have a default style to roll back to when you hit reset.
Take a look at this tutorial if you're unfamiliar with them: http://wpftutorial.net/Styles.html
Do not manipulate UIElements' properties in code. WPF is not winforms. As Yatrix's answer said, implement styles, or even datatemplates and triggers to manipulate different properties of different UIElements acording to some logic (defined in ViewModel or somewhere else). I suggest you to take a look at WPFTutorial.net
FindName is broken for me :(
The object I am looking for is there. I have proof.
Here is the scenario:
ToggleButton button = (ToggleButton)sender;
Popup popup = (Popup)button.FindName("popSelectIteration");
popup is null but not always. Just sometimes. But even when it is set to null the child I am looking for is there.
I put a break point in when it was null and grabbed these two screenshots.
The is where FindName is returning null for "popSelectIteration".
But if you dig into the watch, you see that the child is there.
So what am I missing? Why does FindName not find it? As you can see from the screen shot this is not a timing issue (the FindName watch is null but the direct path is fine).
Is there a better way to find a control?
Side Note: If you are intersted in the XAML for the toggle button in question it can be found in this question: WPF - FrameworkElement - Enumerate all decendents?.
Update: I did some digging to see why this fails some times and other times it works. I have an animation that calls NameScope.SetNameScope((DependencyObject)form, new NameScope()); (Full method code here). Right after that call the FindName starts to fail.
I don't really understand that call. I think I copied and pasted the code. Anyway, I commented it out. But I would love know why this is failing.
I would guess it has to do with the difference between the visual and logical tree. The control is in the logical tree but maybe the template for this control has not been applied yet and therefore FindName won't return anything useful.
You could try to call ApplyTemplate(); on the container first.
This would also explain why it returns something sometimes.
Try
LogicalTreeHelper.FindLogicalNode(button, "popSelectIteration");
Little late to the party (and not actually answer to OP question), but
when you add elements dynamically, they are not findable by FindName.
You need to register them by calling RegisterName.
Example:
string number = GenerateNumber();
Button myButton = new Button();
myButton.Content = number;
myButton.Name = "button_" + number;
RegisterName(myButton.Name, myButton);
Panel.Children.Add(myButton);
object o = Panel.FindName(myButton.Name);
Maybe someone might find this useful.
In my experience, this happens when you add items via code-behind. I've found that you can fool FindName() (or the animation framework) via name scopes. That is, when you create your control, you do
NameScope.GetNameScope(yourContainer).RegisterName("name of your control", yourControlInstance);
For this to work reliably, though, you must make sure that you unregister the name:
NameScope.GetNameScope(yourContainer).UnregisterName("name of your control");
Posting this for future reference.
I have meet the same question now, but I use the method like so:
#region Override - OnApplyTemplate
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.PART_ListViewLeft = GetTemplateChild(cPART_ListViewLeft) as ListView;
this.PART_ListViewCenter = GetTemplateChild(cPART_ListViewCenter) as ListView;
this.PART_ListViewRight = GetTemplateChild(cPART_ListViewRight) as ListView;
this.PART_GridViewLeft = GetTemplateChild(cPART_GridViewLeft) as DsxGridView;
this.PART_GridViewCenter = GetTemplateChild(cPART_GridViewCenter) as DsxGridView;
this.PART_GridViewRight = GetTemplateChild(cPART_GridViewRight) as DsxGridView;
if(this.PART_ListViewLeft!=null)
this.PART_ListViewLeft .AlternationCount = this.AlternatingRowBrushes.Count;
if(this.PART_ListViewCenter!=null)
this.PART_ListViewCenter .AlternationCount = this.AlternatingRowBrushes.Count;
if(this.PART_ListViewRight!=null)
this.PART_ListViewRight .AlternationCount = this.AlternatingRowBrushes.Count;
// ApplyTempleted = true;
CreateColumnLayout();
}
#endregion
If the Control is dynamic create and of which or whose container the 'Visibility' is set to hide or Collapsed, then the code this.PART_ListViewLeft = GetTemplateChild(cPART_ListViewLeft) as ListView; will always return null, the reason is that the datatemplete has not yet been applied before OnApplyTemplate being called.
I would suggest to avoid using FindName function, based on my experience, expecially problematic when you try to find something in the DataTemplate applied to some control.
Instead , if it possible (based on your software architecture) declare Popup in XAML and
refer to it like resource or use Binding to set some Model property to it's reference.
Good luck.
Try to use button.FindResource("popSelectIteration")
ellipseStoryboard.Children.Add(myRectAnimation);
containerCanvas.Children.Add(myPath);
After you add register the controls like
RegisterName("TextBlock1", Var_TextBox);
or
RegisterName(myRectAnimation.Name,myRectAnimation);
RegisterName(myPath.Name,myPath);
I am trying to disable a number of text boxes intended for displaying data (not edit) in one of my UserControls. However, for some reason I can not get the textBoxes to disable properly.
I've set "ApplyAuthorization on readWriteAuthorization" to true and the textBoxes are databound to the correct properties.
I've also added the following lines to the CanWriteProperty of my object:
if (propertyName == OpeningDateProperty.Name) return false;
if (propertyName == ChangeDateProperty.Name) return false;
if (propertyName == CloseDateProperty.Name) return false;
return base.CanWriteProperty(propertyName);
I can't figure out what I'm doing wrong here. I've implemented pretty much the same thing recently in other UserControls without any problems...
I am using Windows Forms in C# .NET (Visual Studio 2008)
EDIT: The code snippets and the properties are taken from my customer object. The date represent opening, last change and closure of the customer account. They are never supposed to be edited at all and in fact in the old sollution they are represented by textLabels, however we now want to use a text box and make the property's CanWriteProperty false.
I realise that the information might be sort of scarce, but I am looking for what I might have forgotten in this process.
EDIT: We are using CSLA as well and I guess (I'm new at this whole thing) this has something to do with why we want to do it like this.
EDIT (Sollution): As you can see in my answer below, the problem was that I had not set up the CurrentItemChanged event like I should have.
If you're trying to get them to be read only, then just set the .ReadOnly property to true.
Alternatively, if you're never ever using these textboxes for editing, then maybe just use a Label instead?
EDIT: Ahh it appears this more of a CSLA-framework question than a pure windows forms question. I've never even heard of CSLA before this question, but it looks interesting.
If you are databinding to properties of the control just bind the "ReadOnly" property of the textbox to the "CanWrite" property of your business object.
i think you mean ReadOnly property
To make this work you need to do the following:
Make sure the TextBox is databound to the right property in the correct way
Set up the needed checks for each textBox in the CanWriteProperty override in your root object
if (propertyName == OpeningDateProperty.Name) return false;
Make sure the rootBindingsource's CurrentItemChanged event is set up right
private void rootBindingSource_CurrentItemChanged(object sender, EventArgs e)
{
readWriteAuthorization1.ResetControlAuthorization();
}
Make sure the texBox's "ApplyAuthorization on ReadWriteAuthorization" is set to true
This solved the problem for me.