I wanna make a customized window base. So I made a custom window which inherits from Window.
For example:
public class MyWindowBase : Window
{
...
...
}
I wanna override the different Brushes of the super class Window for my own purpose.
In my previous experience, to override methods/properties with no abstract or virtual in the super class need the key word "new".
For example:
public new void DoSomething() { ........ base.DoSomething() ....... }
public new string SomeText { get { ... } set {......} }
It works in my previous work.
However, in this time of dealing with WPF Window, it doesn't work.
I tried to override the different Brushes of the super class Window as follows:
public new Brush BorderBrush
{
get { ... }
set { myBorderBrush = value; base.BorderBrush = null }
}
public new Brush Background
{
get { ... }
set { myBackground = value; base.Backgound = null; }
}
.....
.....
.....
I tried the change the value of the above Brushes in MyWindowBase, it just change the value of the super class Window, it doesn't change the value of myBorderBrush and myBackground.
So, how could I override the methods and properties of the super class Window?
Actually, I want to override the base background so that it will be null or transparent forever, but the changed value will be applied on my own custom Background.
Thank you very much!
If you only want to set the value, then you can set it using
this.BorderBrush = Brushes.Blue;
this.Background = Brushes.Red;
If you wish to overwrite the Property Metadata (for things like default value, property changed logic, or validation), you can use OverrideMetadata
Window.BackgroundProperty.OverrideMetadata(
typeof(MyWindowBase), myPropertyMetadata);
If you simply want to add some logic on Changed, you can use a DependencyPropertyDescriptor
var dpd = DependencyPropertyDescriptor.FromProperty(
Window.BackgroundProperty, typeof(Window));
dpd.AddValueChanged(this.Value, new EventHandler(BackgroundChanged));
private void BackgroundChanged(object sender, EventArgs e)
{
//do the code here
}
And if you're looking to override a Method, and not a Property, then you can use the override keyword
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
}
Related
I'm creating a segment view for iOS and Android with using Xamarin form.
It's working fine if I generate it from code.
But I want to generate it from xaml.
like this.
<local:Segment>
<local:Segmentbutton text="boy">
<local:Segmentbutton text="girl">
<local:Segment>
In my Segment class, it generates buttons and add to lists to control them.
When I generate My Segment with code, looks like this.
List<String> buttonTexts = new List<String>();
buttonTexts.Add ("B");
buttonTexts.Add ("G");
segment = new AXSegment (buttonTexts, 50, Color.FromHex ("#000000"), AppConstants.GlobalColor, Color.FromHex ("#EEEEEE"));
segment.buttonCallback = SexSegmentButtonChanged;
segment.SelectedIndex = 0;
What docs should I look to do it in xaml?
I looked "BindableProperty" or many thins but could not find yet.
I just want trigger SETTER when
<local:Segmentbutton text="boy">
so that I just generate button and add it to my list in MySegment.
Thanks.
ContentPropertyAttribute is the keyword. You have to create:
Segment
Nothing special here, just a Element with a Text property.
public class Segment : Element
{
public static readonly BindableProperty TextProperty = BindableProperty.Create<Segment, string>(p => p.Text, string.Empty);
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
}
SegmentedView
Inherits from View. The attribute ContentProperty tells the Xaml-Interpreter/Compiler to stuff every child node into the property called Segments.
[ContentProperty("Segments")]
public class SegmentedView : View
{
private readonly IList<Segment> _segments = new List<Segment>();
public IList<Segment> Segments
{
get { return _segments; }
}
}
SegmentedViewRender
Your custom renderer inherits from a renderer of your choice. Android.Views.View is the type of the native control. You have to change it.
public class SegmentedViewRender : ViewRenderer<SegmentedView, Android.Views.View>
{
protected override void OnElementChanged(ElementChangedEventArgs<SegmentedView> e)
{
base.OnElementChanged(e);
// segments are in: this.Element.Segments
}
}
Usage
<local:SegmentedView>
<local:Segment Text="Bengal"></local:Segment>
<local:Segment Text="Siam"></local:Segment>
<local:Segment Text="Maine Coon"></local:Segment>
</local:SegmentedView>
I have a need of one DependencyProperty from a View in my ViewModel constructor:
My problem: MEF wouldn't SatisfyImports() 'because it is marked with one or more ExportAttributes' (that is the exception)
This is the code structure for the VIEW:
public class MyView : UserControl
{
[Export(MethodTypes.ChartType)]
public Charts MyChartType
{
get
{
object k = GetValue(ChartTypeProperty);
Charts f = (Charts)Enum.Parse(typeof(Charts), k.ToString(), true);
return f;
}
set
{
SetValue(ChartTypeProperty, value);
}
}
[Import(ViewModelTypes.GenericChartViewModel)]
public object ViewModel
{
set
{
DataContext = value;
}
}
public MyView()
{
InitializeComponent();
if (!ViewModelBase.IsInDesignModeStatic)
{
// Use MEF To load the View Model
CompositionInitializer.SatisfyImports(this);
}
}
}
and the VIEWMODEL:
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(ViewModelTypes.GenericChartViewModel)]
public class GenericChartViewModel
{
[ImportingConstructor]
public GenericChartViewModel([Import(MethodTypes.ChartType)] Charts forChartType)
{
string test = forChartType.ToString();
}
}
Please give me any hints on this or maybe suggest a better solution for passing parameters through mef
In my case, I would need to pass only dependecyproperty's for now...
Thanks
Your work around isn't really good.. can't you remove the export from ChartTypes and pass it manually to whoever wants it? I presume the viewmodel is only one insterested in it..
I managed to put this through !
Instead of the code in the default constructor, I use:
void MyView_Loaded(object sender, RoutedEventArgs e)
{
if (!ViewModelBase.IsInDesignModeStatic)
{
var catalog = new TypeCatalog(typeof(GenericChartViewModel));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
and the dependencyproperty value is correctly propagated to the ViewModel
(must do this after control is loaded, or the property will have its default value)
However, I would be very grateful if someone could:
tell me how generate a catalog from another non-referenced assembly?
Thanks
My goal is to create a custom TextBlock control that has a new dependency property, SearchText. This property will contain a regular expression. All occurrences of this regular expression in the text of the TextBlock will be highlighted using a custom style (another DP).
My current implementation involves clearing all of the Inline objects in the TextBlock's InlineCollection. I then fill the TextBlock with runs for unhighlighted text and runs for highlighted text with the style applied (this method does not support adding inlines directly to the TextBlock, instead TextBlock.TextProperty has to be used).
Works great, but sometimes I get a strange exception when trying to clear the Inlines: InvalidOperationException: "Cannot modify the logical children for this node at this time because a tree walk is in progress."
This problem seems to be related to this one. I am modifying the inlines in the TextChanged function, but I'm using a flag to avoid infinite recursive edits.
Any thoughts on how to architect this custom control? Is there a better way to do this? How do I get around this exception?
Thanks!
In my implementation, I solved this by just adding another dependency property, called OriginalText. When it's modified, I updated both the Text property and update the highlighting. Here's the code:
public class HighlightTextBlock : TextBlock
{
public string HighlightedText
{
get { return (string)GetValue(HighlightedTextProperty); }
set { SetValue(HighlightedTextProperty, value); }
}
public static readonly DependencyProperty HighlightedTextProperty =
DependencyProperty.Register("HighlightedText", typeof(string), typeof(HighlightTextBlock), new UIPropertyMetadata(string.Empty, UpdateHighlightEffect));
public static readonly DependencyProperty OriginalTextProperty = DependencyProperty.Register(
"OriginalText", typeof(string), typeof(HighlightTextBlock), new PropertyMetadata(default(string), OnOriginalTextChanged));
private static void OnOriginalTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var block = ((HighlightTextBlock)obj);
block.Text = block.OriginalText;
block.UpdateHighlightEffect();
}
public string OriginalText
{
get { return (string)GetValue(OriginalTextProperty); }
set { SetValue(OriginalTextProperty, value); }
}
private static void UpdateHighlightEffect(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (!(string.IsNullOrEmpty(e.NewValue as string) && string.IsNullOrEmpty(e.OldValue as string)))
((HighlightTextBlock)sender).UpdateHighlightEffect();
}
private void UpdateHighlightEffect()
{
if (string.IsNullOrEmpty(HighlightedText)) return;
var allText = GetCompleteText();
Inlines.Clear();
var indexOfHighlightString = allText.IndexOf(HighlightedText, StringComparison.InvariantCultureIgnoreCase);
if (indexOfHighlightString < 0)
{
Inlines.Add(allText);
}
else
{
Inlines.Add(allText.Substring(0, indexOfHighlightString));
Inlines.Add(new Run()
{
Text = allText.Substring(indexOfHighlightString, HighlightedText.Length),
Background = Consts.SearchHighlightColor,
});
Inlines.Add(allText.Substring(indexOfHighlightString + HighlightedText.Length));
}
}
private string GetCompleteText()
{
var allText = Inlines.OfType<Run>().Aggregate(new StringBuilder(), (sb, run) => sb.Append(run.Text), sb => sb.ToString());
return allText;
}
}
Still not sure if there's a better way to do this altogether, but I appear to have found a work around.
I was updating the inlines/runs in a function that was fired by the change notification for the TextProperty and the SearchTextProperty.
Now I'm firing the highlight/update code from a Dispatcher.BeginInvoke() call in the change notification with DispatcherPriority.Normal.
In case anyone wants an example of how to do this, I found this
When inheriting a control in Silverlight, how do I find out if its template has already been applied?
I.e., can I reliably get rid of my cumbersome _hasTemplateBeenApplied field?
public class AwesomeControl : Control
{
private bool _hasTemplateBeenApplied = false;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this._hasTemplateBeenApplied = true;
// Stuff
}
private bool DoStuff()
{
if (this._hasTemplateBeenApplied)
{
// Do Stuff
}
}
}
Nope that is the standard way to track whether the template has been applied.
I'm a complete newbie at WPF.
At the moment I'm making a usercontrol for form elements called "LabeledTextbox" which contains a label, a textbox and a textblock for errormessages.
When the using code adds an errormessage, I want to put the border of the textbox in red. But, when the errormessage gets removed, I'd like to turn back to the default bordercolor of the textbox.
I feel there must be a very easy way to do this.
My code:
(in public partial class LabeledTextbox : UserControl)
public string ErrorMessage
{
set
{
if (string.IsNullOrEmpty(value))
{
_textbox.BorderBrush = Brushes.Black; //How do I revert to the original color in the most elegant way?
}
else
{
_textbox.BorderBrush = Brushes.Red;
}
_errorMessage.Text = value;
}
}
You could use
_textBox.ClearValue(TextBox.BorderBrushProperty);
That will remove the directly assigned value and go back to the value defined by the style or template.
You can grab the default colours from the class SystemColors
Here is the list of all system colours:
http://msdn.microsoft.com/de-de/library/system.windows.systemcolors.aspx
Default background colour of the client area:
_textbox.Background = SystemColors.WindowBrush;
Default text colour inside the client area:
_textbox.SystemColors.WindowTextBrush
I may be late to the party, but for future readers, you can also use Button.BackgroundProperty.DefaultMetadata.DefaultValue for this purpose. This is especially useful when you're using a Converter where you need to return a value and therefore cannot use ClearValue() call.
Does this work? Setting it to black is better than using the ClearValue method
public string ErrorMessage
{
set
{
if (string.IsNullOrEmpty(value))
{
_textbox.Background = Brushes.Black;
}
else
{
_textbox.Background = Brushes.Red;
}
_errorMessage.Text = value;
}
}
Just store the default settings. Here a code excample.
System.Windows.Media.Brush save;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Store the default background
save = testButton.Background;
}
private void ChangeBackground(){
testButton.Background = Brushes.Red;
}
private void restoreDefaultBackground(){
//Restore default Backgroundcolor
testButton.Background = save;
}