How to place expander chevron on the left? - xaml

By default the expander chevron is placed on the right:
<Expander
Header="This text is in the header"
Content="This is in the content"/>
How can I make the chevron be on the left of the header text?
Looking at the Expander class docs I wasn't able to find promising ExpanderChevron styles.
Looking at the Live Visual Tree I can see the chevron is in the second column of a grid. I looked at trying to override the column via a style, but couldn't figure it out.

You have two options.
The first option is to create a custom template. You could copy the default template for the ToggleButton from GitHub and edit as per your requirements.
The second option is to create your own custom Expander control that extends the existing one and programmatically move the elements:
public class CustomExpander : Expander
{
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
ToggleButton tb = GetTemplateChild("ExpanderHeader") as ToggleButton;
if (tb != null)
tb.Loaded += TbLoaded;
}
private void TbLoaded(object sender, RoutedEventArgs e)
{
ToggleButton tb = (ToggleButton)sender;
tb.Padding = new Thickness(0, 0, 16, 0);
tb.Loaded -= TbLoaded;
ContentPresenter cp = FindVisualChild<ContentPresenter>(tb);
Border border = FindVisualChild<Border>(tb);
if (cp != null && border != null)
{
Grid.SetColumn(cp, 1);
Grid.SetColumn(border, 0);
border.Margin = new Thickness(8, 0, 20, 0);
}
}
private static T FindVisualChild<T>(DependencyObject visual) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(visual, i);
if (child != null)
{
T correctlyTyped = child as T;
if (correctlyTyped != null)
return correctlyTyped;
T descendent = FindVisualChild<T>(child);
if (descendent != null)
return descendent;
}
}
return null;
}
}
Don't forget to update your XAML markup to use your custom control:
<local:CustomExpander Header="This text is in the header" Content="This is in the content" />

Related

Navigate Hamburger Button to MasterDetailPage.Master Part?

I have a hamburger icon which I will use it to navigate masterdetailpage.master part. I couldnt make sure how to write syntax under of the click event. Any ideas?
You could use the custom renderer to reset the hamburger icon of imagebutton.
[assembly: ExportRenderer(typeof(MainPage ), typeof(MasterDetailPageRenderer_Droid))]
namespace MasterDetailPageDemo.Droid
{
class MasterDetailPageRenderer_Droid:MasterDetailPageRenderer
{
public Android.Support.V7.Widget.Toolbar toolbar;
public Activity context;
public MasterDetailPageRenderer_Droid(Context context) : base(context)
{
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
context = (Activity)Forms.Context;
toolbar = context.FindViewById<Android.Support.V7.Widget.Toolbar>(Droid.Resource.Id.toolbar);
if (toolbar != null)
{
for (var i = 0; i < toolbar.ChildCount; i++)
{
var imageButton = toolbar.GetChildAt(i) as Android.Widget.ImageButton;
var drawerArrow = imageButton?.Drawable as DrawerArrowDrawable;
if (drawerArrow == null)
continue;
imageButton.SetImageDrawable(Forms.Context.GetDrawable(Resource.Drawable.star_small));
}
}
}
}

Change highlight color of NSTableView selected row

how to change NSTable selected row background color?
here is good answer, but it is for uitable view .
For now,what I see is that I can change selected hilight style :
MyTAble.SelectionHighlightStyle = NSTableViewSelectionHighlightStyle.Regular;
But here is only 3 options;
None = -1L,
Regular,
SourceList
I have tried following solution :
patientListDelegate.SelectionChanged += (o, e) => {
var r = PatientTableView.SelectedRow;
var v = PatientTableView.GetRowView (r, false);
v.Emphasized = false;
};
It works normally , but if I minimize and then open application again , still shows blue color
I found answer in objective-c
Change selection color on view-based NSTableView
Here is c# implementation:
inside delegate :
public override NSTableRowView CoreGetRowView (NSTableView tableView, nint row)
{
var rowView = tableView.MakeView ("row", this);
if (rowView == null) {
rowView = new PatientTableRow ();
rowView.Identifier = "row";
}
return rowView as NSTableRowView;
}
and custom row :
public class PatientTableRow : NSTableRowView
{
public override void DrawSelection (CGRect dirtyRect)
{
if (SelectionHighlightStyle != NSTableViewSelectionHighlightStyle.None) {
NSColor.FromCalibratedWhite (0.65f, 1.0f).SetStroke ();
NSColor.FromCalibratedWhite (0.82f, 1.0f).SetFill ();
var selectionPath = NSBezierPath.FromRoundedRect (dirtyRect, 0, 0);
selectionPath.Fill ();
selectionPath.Stroke ();
}
}
}

How to detect ListView is scrolling up or down

Is there a way to detect that ScrollViwer of ListView is in scrolling mode and stopped scrolling. In windows phone 8.1 ListView we can not get reference of the scrollviewer.
Any one done it in windows phone 8.1 WinRT app?
Once the ListView is Loaded you can get the ScrollViewer like this:
var sv = (ScrollViewer)VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(this.ListV, 0), 0);
Edit
As Romasz suggested, once you get the ScrollViewer, you can use its ViewChanged event, to monitor when it is scrolling and when it stops.
Also, here's the generic extension method that I use for traversing the visual tree:
// The method traverses the visual tree lazily, layer by layer
// and returns the objects of the desired type
public static IEnumerable<T> GetChildrenOfType<T>(this DependencyObject start) where T : class
{
var queue = new Queue<DependencyObject>();
queue.Enqueue(start);
while (queue.Count > 0) {
var item = queue.Dequeue();
var realItem = item as T;
if (realItem != null) {
yield return realItem;
}
int count = VisualTreeHelper.GetChildrenCount(item);
for (int i = 0; i < count; i++) {
queue.Enqueue(VisualTreeHelper.GetChild(item, i));
}
}
}
To get the ScrollViewer using this methos, do this:
var sv = yourListView.GetChildrenOfType<ScrollViewer>().First();
You can find the ScrollViewer of your ListView by using VisualTreeHelper. For example like this:
// method to pull out a ScrollViewer
public static ScrollViewer GetScrollViewer(DependencyObject depObj)
{
if (depObj is ScrollViewer) return depObj as ScrollViewer;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = GetScrollViewer(child);
if (result != null) return result;
}
return null;
}
Once you have a ScrollViewer you can subscribe to its events:
GetScrollViewer(yourListView).ViewChanged += yourEvent_ViewChanged;
You must load data to listview before getting scrollview. If listview has empty row then scrollview you get will be null.

Access to controls inside a FlipView un XAML

In my Windows 8 app, I am trying to change the text of a textblock inside a DataTemplate of a FlipView...
my FlipView datatemplate (simplified...) :
<FlipView.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="test" />
</DataTemplate>
</FlipView.ItemTemplate>
I tried this solution : How do I access a control inside a XAML DataTemplate?
So my .cs :
var _Container = flipView.ItemContainerGenerator.ContainerFromItem(flipView.SelectedItem);
var _Children = AllChildren(_Container);
var myTextBlock= _Children.OfType<TextBlock>().FirstOrDefault(c => c.Name.Equals("test"));
myTextBlock.Text = "test";
with the method :
public List<Control> AllChildren(DependencyObject parent)
{
var _List = new List<Control>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is Control)
_List.Add(_Child as Control);
_List.AddRange(AllChildren(_Child));
}
return _List;
}
But I get a NullReferenceException error : "{"Object reference not set to an instance of an object."}"
So it doesn't find my textblock...
Thanks
hello friend i have checked your code..and what i found is a very unnoticeable mistake..that is about the Control keyword..actually it is your type of control you want to search in your flipview..like textblock,textbox etc...you have to just change your AllChilderen Function like this and then all will work fine..
public List<TextBlock> AllChildren(DependencyObject parent)
{
var _List = new List<TextBlock>();
int j = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is TextBlock)
_List.Add(_Child as TextBlock);
_List.AddRange(AllChildren(_Child));
}
return _List;
}
hope it will help you..

How to have a custom side bar slide from right in your metro app?

I am developing a metro app where , I want to have a customized bar slide when the user clicks on something in the screen.
This is what i am talking about :-
--------------
| | |
| | | <----
| | |
---------------
main screen side
bar
In this side bar i want have some simple controls like image and text block etc..
1) How can i do this , any help would be appreciated
2) Its not against metro principles right ?
You could try to leverage a flyout like the settings flyout. You might want to look at Callisto.
You could otherwise include a Xaml element that above all others and toggle it's visibility along with a positioning right on the screen. How to position depends on what root element you're using. For positioning an element in a canvas add Canvas.Right="0" to the child element.
You can use PopUp.
have a look at this example,
http://code.msdn.microsoft.com/windowsapps/App-settings-sample-1f762f49
you can use following helper
WinRT Flyout Helper
public class FlyoutHelper
{
protected Popup m_Popup = new Popup();
public Popup Show(Popup popup, FrameworkElement button, double offset = 35d)
{
if (popup == null)
throw new Exception("Popup is not defined");
m_Popup = popup;
if (button == null)
throw new Exception("Button is not defined");
if (double.IsNaN(offset))
throw new Exception("Offset is not defined");
var _Child = popup.Child as FrameworkElement;
if (_Child == null)
throw new Exception("Popup.Child is not defined");
if (double.IsNaN(_Child.Height))
throw new Exception("Popup.Child.Height is not defined");
if (double.IsNaN(_Child.Width))
throw new Exception("Popup.Child.Width is not defined");
// get position of the button
var _Page = Window.Current.Content as Page;
var _Visual = button.TransformToVisual(_Page);
var _Point = _Visual.TransformPoint(new Point(0, 0));
var _Button = new
{
Top = _Point.Y,
Left = _Point.X,
Width = button.ActualWidth,
Height = button.ActualHeight,
};
// determine location
var _TargetTop = (_Button.Top + (_Button.Height / 2)) -
_Child.Height - offset;
var _TargetLeft = (_Button.Left + (_Button.Width / 2)) -
(_Child.Width / 2);
if ((_TargetLeft + _Child.Width) > Window.Current.Bounds.Width)
_TargetLeft = Window.Current.Bounds.Width - _Child.Width - offset;
if (_TargetLeft < 0)
_TargetLeft = offset;
// setup popup
popup.VerticalOffset = _TargetTop;
popup.HorizontalOffset = _TargetLeft;
// add pretty animation(s)
popup.ChildTransitions = new TransitionCollection
{
new EntranceThemeTransition
{
FromHorizontalOffset = 0,
FromVerticalOffset = 20
}
};
// setup
m_Popup.IsLightDismissEnabled = true;
m_Popup.IsOpen = true;
// handle when it closes
m_Popup.Closed -= popup_Closed;
m_Popup.Closed += popup_Closed;
// handle making it close
Window.Current.Activated -= Current_Activated;
Window.Current.Activated += Current_Activated;
// return
return m_Popup;
}
protected void Current_Activated(object sender, WindowActivatedEventArgs e)
{
if (m_Popup == null)
return;
if (e.WindowActivationState == CoreWindowActivationState.Deactivated)
m_Popup.IsOpen = false;
}
protected void popup_Closed(object sender, object e)
{
Window.Current.Activated -= Current_Activated;
if (m_Popup == null)
return;
m_Popup.IsOpen = false;
}
}
For this you can use CharmFlyout.
Here is the sample code http://code.msdn.microsoft.com/windowsapps/CharmFlyout-A-Metro-Flyout-25fe53b6