I have an attached property for a Grid. It is used to automatically lay out the content controls inside the Grid. What is basically does is that it goes through the Children collection and puts every control in the next free cell of the Grid control. It is done once (in the Initialized event handler).
public static readonly DependencyProperty AutoLayoutProperty =
DependencyProperty.RegisterAttached(
"AutoLayout",
typeof(bool),
typeof(GridEx),
new UIPropertyMetadata(false, OnAutoLayoutChanged));
private static void OnAutoLayoutChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var grid = d as Grid;
if (!grid.IsInitialized)
grid.Initialized += new EventHandler(grid_Initialized);
else { UpdateLayout(grid); }
}
private static void UpdateLayout(Grid grid)
{
foreach(var child in grid.Children)
{
// Set Grid.Column and Grid.Row properties on the child
}
}
This code works and does everything I need, yet there is one problem - when I edit the contents of the grid in Expression Blend designer those Grid.Column and Grid.Row properties on child controls get reset. It is just annoying. What can I do to detect the refresh of the Blend designer and reapply those attached properties to grid children?
Try with the Loaded event instead.
Edit - Added workaround for designer
private static void OnAutoLayoutChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var grid = d as Grid;
grid.Loaded += (object sender, RoutedEventArgs e2) =>
{
UpdateLayout(grid);
};
// Workaround for Blend..
if (DesignerProperties.GetIsInDesignMode(grid) == true)
{
grid.LayoutUpdated += (object sender, EventArgs e2) =>
{
UpdateLayout(grid);
};
}
}
Related
Is there any feature of the TextBlock that allows scrolling to the end always?
I've seen a number of examples that do this in the code behind,
I want to keep the principle of MVVM and not touch the code behind,
I'm looking for a way to do this in XAML.
Have one?
I am assuming your TextBlock is nested within a ScrollViewer. In this case you are going to have to create an attached property. See this related question:
How to scroll to the bottom of a ScrollViewer automatically with Xaml and binding?
i.e. create an attached property:
public static class Helper
{
public static bool GetAutoScroll(DependencyObject obj)
{
return (bool)obj.GetValue(AutoScrollProperty);
}
public static void SetAutoScroll(DependencyObject obj, bool value)
{
obj.SetValue(AutoScrollProperty, value);
}
public static readonly DependencyProperty AutoScrollProperty =
DependencyProperty.RegisterAttached("AutoScroll", typeof(bool), typeof(Helper), new PropertyMetadata(false, AutoScrollPropertyChanged));
private static void AutoScrollPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var scrollViewer = d as ScrollViewer;
if (scrollViewer != null && (bool)e.NewValue)
{
scrollViewer.ScrollToBottom();
}
}
}
Then bind as follows:
<ScrollViewer local:Helper.AutoScroll="{Binding BooleanViewModelPropertyThatTriggersScroll}" .../>
I have a textbox on my wpfgrid that I need to perform some tekst inserting and such on. To do this, the textbox is referenced into the presentationmodel from the view, ReferenceToTextBox (we do MVP with Prism). Also, the textbox in the view TextDescription is bound to the Description-property on the model.
We also have a dropdown-list containing some predefined text-blobs (adresses, VAT-numbers and such). When you choose one of these, they should be inserted into the textbox at the carets current position. Since you can't bind on CaretIndex, the above mentioned workaround is made. The dropdown-list is bound on SelectedItem to a property on the model, so when the SelectedItem changes, the property changes, and in the setter on the property a method is called to insert the text of the selected-item into the ReferenceToTextBox "virtual" textbox in the model (which should be just a reference to the textbox in the view).
However, if I delete all the text from the textbox in the view and add a new predefined text-blob. The ReferenceToTextBox.Text property still contains all the text that I deleted. It seems like the ReferenceToTextBox is no longer just a reference, but a whole own textbox. Which makes it even weirder when updates to ReferenceToTextBox.Text actually updates the "visual" textbox on the view.
What is actually happening here?
Not a direct answer to your question, but what about a derived TextBox class that actually allows binding to its CaretIndex property:
public class TextBoxEx : TextBox
{
public static readonly DependencyProperty CaretIndexProperty = DependencyProperty.Register(
"CaretIndex", typeof(int), typeof(TextBoxEx),
new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, CaretIndexChanged));
public new int CaretIndex
{
get { return (int)GetValue(CaretIndexProperty); }
set { SetValue(CaretIndexProperty, value); }
}
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
CaretIndex = base.CaretIndex;
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
CaretIndex = base.CaretIndex;
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
CaretIndex = base.CaretIndex;
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
CaretIndex = base.CaretIndex;
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
CaretIndex = base.CaretIndex;
}
private static void CaretIndexChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (obj is TextBox)
{
((TextBox)obj).CaretIndex = (int)e.NewValue;
}
}
}
Sorry for my English.
I try to write UserControl (SearchTextBox...simmillar Firefox search textbox) that consists from TextBox, Popup and ListBox in a Popup. I need to change ItemsSource of ListBox dynamically in my application. So i use DependencyProperty in UserControl:
//STextBox UserControl Code-Behind
public partial class STextBox : UserControl
{
public static readonly DependencyProperty ItemsSourceProperty;
static STextBox()
{
ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(STextBox),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsArrange, new PropertyChangedCallback(OnItemsSourceChanged)));
}
public IEnumerable ItemsSource
{
get
{
return (IEnumerable)GetValue(STextBox.ItemsSourceProperty);
}
set
{
SetValue(STextBox.ItemsSourceProperty, value);
}
}
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
STextBox c = (STextBox)d;
c.ItemsSource = (IEnumerable)e.NewValue;
}
I can't use bindings to ItemsSource in my app, because two lists for my ListBox-ItemsSource creates on the fly from records of database. I set ItemsSource in code:
//my app code-behind
switch (SomeIF)
{
case 0:
sTextBox.ItemsSource = list1;
break;
case 1:
sTextBox.ItemsSource = list2;
break;
}
But nothing happened. I know exactly that OnItemsSourceChanged method is fired, but new value never assigned to ItemsSource. What I'am doing wrong?
Can not say that I liked, but this solution work.
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
STextBox c = (STextBox)d;
c.OnItemsSourceChanged(e);
}
//added overload method where I can simply set property to the control
protected virtual void OnItemsSourceChanged(DependencyPropertyChangedEventArgs e)
{
myListBox.ItemsSource = ItemsSource;
}
I have a ListBox that uses a WrapPanel for its ItemsPanel, a custom ItemTemplate, and a custom ItemContainerStyle. The ItemContainerStyle's template contains a selection box that shows up when an item is selected. The graphics designer would like this selection box to overlap sibling items in the ListBox like it's an overlay.
The first thing I tried was setting the Canvas.ZIndex property of the ItemContainer in the Selected state. That did not seem to have an effect. Then I read that list items might be wrapped inside of a ContentPresenter, so I created an attached property that changes the ZIndex of an item's parent, but then I found out Silverlight storyboards don't let you animate custom attached properties.
Does anyone know of a technique we can use to achieve the effect we desire?
I found a solution. Basically, I created an attached property that sets up an event handler on any Selector (including ListBox) for when its selection changes. When it changes, the code iterates through all of the item containers, adjusting the Canvas.ZIndex based on whether the container represents the selected item:
public static readonly DependencyProperty SetZIndexOnSelectionProperty = DependencyProperty.RegisterAttached(
"SetZIndexOnSelection", typeof(bool), typeof(FrameworkUtils),
new PropertyMetadata(zIndexSettingChanged));
public static bool GetSetZIndexOnSelection(DependencyObject obj)
{
return (bool)obj.GetValue(SetZIndexOnSelectionProperty);
}
public static void SetSetZIndexOnSelection(
DependencyObject obj, bool value)
{
obj.SetValue(SetZIndexOnSelectionProperty, value);
}
private static void zIndexSettingChanged(
DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
if (obj is Selector)
{
var selector = obj as Selector;
selector.SelectionChanged += (s, e) =>
{
if (selector.SelectedItem != null)
{
foreach (var pair in selector.GetItemsAndContainers())
{
pair.Value.SetValue(
Canvas.ZIndexProperty,
(pair.Key == selector.SelectedItem) ? 1 : 0);
}
}
};
}
}
I'm new to WPF.
I have like 15 grids on my Window and I have a small menu on which I can click and choose which grid to show up or hide. One grid at a time only. I would like that grid to hode (fade out) when I hit Esc. I have all the animations already, I just need to know what grid is visible (active) at the moment.
I don't know how to get current topmost control of my Window.
My solution is when KeyDown event is triggered on my Window to:
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Escape)
{
//check all grids for IsVisible and on the one that is true make
BeginStoryboard((Storyboard)this.FindResource("theVisibleOne_Hide"));
}
}
By active, I assume that means the one that has keyboard focus. If so, the following will return the control that currently has keyboard input focus:
System.Windows.Input.Keyboard.FocusedElement
You could use it like this:
if (e.Key == System.Windows.Input.Key.Escape)
{
//check all grids for IsVisible and on the one that is true make
var selected = Keyboard.FocusedElement as Grid;
if (selected == null) return;
selected.BeginStoryboard((Storyboard)this.FindResource("HideGrid"));
}
An approach that would be more decoupled would be to create a static attached dependency property. It could be used like this (untested):
<Grid local:Extensions.HideOnEscape="True" .... />
A very rough implementation would look like:
public class Extensions
{
public static readonly DependencyProperty HideOnEscapeProperty =
DependencyProperty.RegisterAttached(
"HideOnEscape",
typeof(bool),
typeof(Extensions),
new UIPropertyMetadata(false, HideOnExtensions_Set));
public static void SetHideOnEscape(DependencyObject obj, bool value)
{
obj.SetValue(HideOnEscapeProperty, value);
}
public static bool GetHideOnEscape(DependencyObject obj)
{
return (bool)obj.GetValue(HideOnEscapeProperty);
}
private static void HideOnExtensions_Set(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grid = d as Grid;
if (grid != null)
{
grid.KeyUp += Grid_KeyUp;
}
}
private static void Grid_KeyUp(object sender, KeyEventArgs e)
{
// Check for escape key...
var grid = sender as Grid;
// Build animation in code, or assume a resource exists (grid.FindResource())
// Apply animation to grid
}
}
This would remove the need to have code in codebehind.