How to make a GridViewColumn readonly/disabled? - c#

I have a reference to a GridViewColumn in my xaml code as RuntimeColumn but I am not able to disable it or set it to readonly programmatically. I will need to do this at runtime without databinding.
I tried:
this.RuntimeColumn.IsEnabled = false;
this.RuntimeColumn.ReadOnly = false;
Any ideas?

You will have to set an EventSetter with Loaded Event, and in your code behind put the following
private void GridViewColumnHeader_Loaded(object sender, RoutedEventArgs e)
{
GridViewColumnHeader columnHeader = sender as GridViewColumnHeader;
Border HeaderBorder = columnHeader.Template.FindName("HeaderBorder", columnHeader) as Border;
if (HeaderBorder != null)
{
HeaderBorder.Background = HeaderBorder.Background;
}
Border HeaderHoverBorder = columnHeader.Template.FindName("HeaderHoverBorder", columnHeader) as Border;
if (HeaderHoverBorder != null)
{
HeaderHoverBorder.BorderBrush = HeaderHoverBorder.BorderBrush;
}
Rectangle UpperHighlight = columnHeader.Template.FindName("UpperHighlight", columnHeader) as Rectangle;
if (UpperHighlight != null)
{
UpperHighlight.Visibility = UpperHighlight.Visibility;
}
Thumb PART_HeaderGripper = columnHeader.Template.FindName("PART_HeaderGripper", columnHeader) as Thumb;
if (PART_HeaderGripper != null)
{
PART_HeaderGripper.Background = PART_HeaderGripper.Background;
PART_HeaderGripper.Cursor = System.Windows.Input.Cursors.Arrow; // override the size curser
}
}

Related

Xamarin Forms Listview selecteditem set backgroundcolor not working first time

I have a CustomViewRenderer, which changes the Backgroundcolor of an item in a Listview.
If I click on an item the Color changes correct.
When I load my ListView and set programmatically the ItemSource and the SelectedItem the Color is Standard Orange in Android.
The ListView.SelectedItem doesnt call the Android Renderer "OnCellPropertyChanged", at the first Time loading.
What is my mistake?
Loading ListView:
listNew = listDG.Where(g => g.check == true).ToList();
listNew = listNew.OrderBy(g => g.beschreibung).ToList();
listGegenstandZuordnungBinding = new BindingList<db.gegenstaendezuordnungliste>(listNew);
lvGegenstaende.ItemsSource = listGegenstandZuordnungBinding;
lvGegenstaende.ItemSelected += LvGegenstaende_ItemSelected; ;
if (App.idgegenstandueb != 0)
{
List<db.gegenstaendezuordnungliste> listSelected = listNew.Where(g => g.idgegenstandueb == App.idgegenstandueb).ToList();
if (listSelected.Count > 0)
{
lvGegenstaende.SelectedItem = listSelected[0];
}
else
{
if (listNew.Count > 0)
{
lvGegenstaende.SelectedItem = listNew[0];
App.idgegenstandueb = listNew[0].idgegenstandueb;
}
}
}
else if (listNew.Count > 0)
{
App.idgegenstandueb = listNew[0].idgegenstandueb;
lvGegenstaende.SelectedItem = listNew[0];
}
Android Renderer:
protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
{
var listView = parent as Android.Widget.ListView;
_selected = false;
if (listView != null)
{
// // Disable native cell selection color style - set as *Transparent*
listView.SetSelector(Android.Resource.Color.Transparent);
listView.CacheColorHint = Xamarin.Forms.Color.Transparent.ToAndroid();
}
var cell = base.GetCellCore(item, convertView, parent, context);
return cell;
}
protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnCellPropertyChanged(sender, e);
_selected = !_selected;
var extendedViewCell = sender as CustomControl;
if (extendedViewCell.Parent.AutomationId != null)
{
if (extendedViewCell.Parent.AutomationId.ToString() == "Listgegenstaende")
{
if (_selected)
extendedViewCell.View.BackgroundColor = Color.FromHex("4682B4");
else
extendedViewCell.View.BackgroundColor = Color.FromHex("#5096D0");
}
}
else
{
if (_selected)
extendedViewCell.View.BackgroundColor = Color.Black;
else
extendedViewCell.View.BackgroundColor = Color.Transparent;
}
}

How to remove/delete ContentControl in Canvas WPF

i'm starting with WPF and i have a problem. Please help me. Thanks, sorry for my bad English!
I have added ContentControl to a Canvas, and i want to remove/delete it.
Draw ContentControl code:
ContentControl cc = new ContentControl();
cc.Content = shape;
cc.Height = h;
cc.Width = w;
Style s = myCanvas.FindResource("DesignerItemStyle") as Style;
cc.Style = s;
Canvas.SetLeft(cc, x);
Canvas.SetTop(cc, y);
myCanvas.Children.Add(cc);
I use HitTest to remove it but i can remove only shape
private void myCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
Point pt = e.GetPosition((Canvas)sender);
HitTestResult result = VisualTreeHelper.HitTest(myCanvas, pt);
if (result != null)
{
myCanvas.Children.Remove(result.VisualHit as Shape); //it works with shape
// i have changed it into myCanvas.Children.Remove(result.VisualHit as ContentControl);
//but it didn't work with ContentControl
}
}
It is so because the ContentControl is the parent of the Shape, and the Canvas's children contains the ContentControl that hosts the shape.
You could do this to fix your issue :)
private void myCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DependencyObject ob = FindAncestor<ContentControl>((DependencyObject)e.Source);
if (ob.GetType() == typeof(ContentControl))
myCanvas.Children.Remove(ob as ContentControl);
}
public T FindAncestor<T>(DependencyObject dependencyObject)
where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindAncestor<T>(parent);
}
I suggest you the next solution:
private void myCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
var dObj = sender as DependencyObject;
if(dObj == null) return;
var ob = dObj.GetChildOfType<ContentControl>();
if (ob != null)
{
myCanvas.Children.Remove(ob);
}
}
Helper code:
public static T GetChildOfType<T>(this DependencyObject depObj)
where T : DependencyObject
{
if (depObj == null) return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = (child as T) ?? GetChildOfType<T>(child);
if (result != null) return result;
}
return null;
}
just put the helper inside the public static class as it is an extension method
regards
You can add the control, for example:
Rectangle r = new Rectangle() { Name = "MyName" };
and to remove from the canvas:
UIElement element = StationLayout.FindName ("MyName") as UIElement;
StationLayout.Children.Remove(element);

How to select an item from a ListBox without using SelectionChange event

I am Silverlight developer and coding in C# to select an item from a list and display the selected item in the textBlock nearby.
My code to do so is:
ListBox lines = new ListBox();
TextBlock txtblkShowSelectedValue = new TextBlock();
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
lines.ItemsSource = param.Component.Attributes.Items;
Grid.SetColumn(lines, 1);
Grid.SetRow(lines, LoopCount);
childGrid.Children.Add(lines);
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;
The problem is how to select a value and how to display it in textblock "txtblkShowSelectedValue " ? because I cannot declare textblock and List variable globally because of current condition if I use selectionChange event
EDIT: The current scenario is :(lines (List) is in different function so it's not in scope of List_SelectionChanged() function)
private static Grid GenerateList(Parameter param, int LoopCount, Grid g)
{
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.ColumnDefinitions.Add(colDef2);
childGrid.ColumnDefinitions.Add(colDef3);
TextBlock txtblk1ShowStatus = new TextBlock();
TextBlock txtblkLabel = new TextBlock();
ListBox lines = new ListBox();
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
lines.ItemsSource = param.Component.Attributes.Items;
Grid.SetColumn(lines, 1);
Grid.SetRow(lines, LoopCount);
childGrid.Children.Add(lines);
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;
lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged);
lines.SelectedIndex = lines.Items.Count - 1;
g.Children.Add(childGrid);
return (g);
}
static void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("clist _SelectionChanged1");
TextBlock txtblk1ShowStatus = new TextBlock();
txtblk1ShowStatus.Text = lines[(sender as ListBox).SelectedIndex];
}
This could be streamlined, but should work as a quick 'n dirty example of one way to solve the problem...
void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Grid g = null;
ListBox lb = sender as ListBox;
if (lb != null && lb.SelectedIndex >= 0)
{
// Find the top-level grid
var parent = VisualTreeHelper.GetParent(lb);
while (parent != null)
{
if (parent.GetType() == typeof(Grid))
{
if ((parent as Grid).Name.Equals("LayoutRoot"))
{
g = (Grid)parent;
break;
}
}
parent = VisualTreeHelper.GetParent(parent);
}
// Found the LayoutRoot, find the textblock
if (g != null)
{
for (int i = 0; i < g.Children.Count; i++)
{
var child = VisualTreeHelper.GetChild(g, i);
if (child is TextBlock)
{
(child as TextBlock).Text = (string)lb.SelectedItem;
break;
}
}
}
}
}
You could also name your textblock and search for that (as I did for "LayoutRoot").
Obviously, this code assumes the textblock is a child of the top-level Grid. Implementing a recursive search wouldn't be difficult.
lines.SelectionChanged+=new System.EventHandler(this.UpdateTextBlock); // add selectionchanged even for your listbox;
private void UpdateTextBlock(object sender, SelectionChangedEventArgs e)
{
txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex].ToString(); // just edit the content of your texblock
}
EDIT : thank you, and sorry to be late :-)
try this :
add parameter for the function, as this :
lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged)
change parameter of this function and set your textblock as this :
static void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("clist _SelectionChanged1");
txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex].ToString()
}
Afteralli solved the problem like this:
lines.SelectionChanged += (o, e) =>
{
MessageBox.Show("clist _SelectionChanged1");
txtblk1ShowStatus.Text = lines.SelectedItem.ToString();
};
lines.SelectedIndex = lines.Items.Count - 1;
in my function GenerateList(..)

c#: show tooltip on subitem

I have a listview, and in one of the columns (not the first) I want to display an error code.
What I haven't been able to do is get the ToolTip to display. I have
this.lstList.ShowItemToolTips = true;
...
ListViewItem value = lstList.Items.Add(name, name, 0);
...
if (lstList.Columns.Contains(lstColErrorCode))
{
value.SubItems.Add(new ListViewItem.ListViewSubItem(value, errorCode.ToString()));
value.ToolTipText = errorCode.ToString("X");
}
I would like to get the hex value of the code to be shown on the tooltip above the decimal value, but it shows above the name.
I haven't been able to get anything I tried to work (like trying to get the coordinates of the subitem). I would appreciate any suggestion.
this code works for me
ToolTip toolTip1 = new ToolTip();
void initMethod()
{
lstList.MouseMove += new MouseEventHandler(lstList_MouseMove);//mousemove handler
this.lstList.ShowItemToolTips = true;
toolTip1.SetToolTip(lstList,"");// init the tooltip
...
ListViewItem value = lstList.Items.Add(name, name, 0);
...
if (lstList.Columns.Contains(lstColErrorCode))
{
ListViewItem.ListViewSubItem lvs = value.SubItems.Add(new ListViewItem.ListViewSubItem(value, errorCode.ToString()));
lvs.Tag = "mydecimal"; // only the decimal subitem will be tooltiped
}
}
the mousemove event from the listview:
void lstList_MouseMove(object sender, MouseEventArgs e)
{
ListViewItem item = lstList.GetItemAt(e.X, e.Y);
ListViewHitTestInfo info = lstList.HitTest(e.X, e.Y);
if ((item != null) && (info.SubItem != null) && (info.SubItem.Tag!=null) && (info.SubItem.Tag.ToString() == "mydecimal"))
{
toolTip1.SetToolTip(lstList,((decimal)info.SubItem.Text).ToString("X"));
}
else
{
toolTip1.SetToolTip(lstList, "");
}
}

How do I force showing a tooltip in WPF

I'd like to show a tooltip when I move the mouse.
Here is my code:
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
Grid grid = (Grid) sender;
if (e.GetPosition(grid).X < 100)
grid.ToolTip = e.GetPosition(grid).X.ToString();
else
grid.ToolTip = null;
}
However, the tooltip disappears after I click on the grid.
Is there a way to force showing the tooltip?
var oldTT = SomeElement.ToolTip as ToolTip;
if (oldTT != null) oldTT.IsOpen = false;
SomeElement.ToolTip = new ToolTip
{
Content = "Lalalalala",
IsOpen = true,
};
or
var tt = SomeElement.ToolTip as ToolTip;
if (tt != null) tt.IsOpen = true;
TooltipService.ShowDuration works, but you must set it on the object having the Tooltip, like this:
<Label ToolTipService.ShowDuration="120000" Name="lblTooltip" Content="Shows tooltip">
<Label.ToolTip>
<ToolTip>
<TextBlock>Hi world!</TextBlock>
</ToolTip>
</Label.ToolTip>

Categories