How to adjust arrow besides a TreeViewItem? - c#

In this tutorial a nice example of a TreeView is shown.
Now I would like to have two elements stacked with a StackPanel within a TreeViewItem vertically:
var treeViewItem = new TreeViewItem();
var stackPanel = new StackPanel() { Orientation = Orientation.Vertical };
stackPanel.Children.Add(... some text ...);
stackPanel.Children.Add(... some icon ...);
treeViewItem.Header = stackPanel;
The little arrow besides the TreeViewItem (to display its subitems) is now placed in the middle - between the icon and the text.
Question: Is it possible to adjust the arrow to the top (besides the icon)?

Related

WPF Border Child visibility have no effect

I need to hide a TextBlock that is child of a Border and is added to a Grid. The following code dynamically add the Border and the TextBlock to the Grid. Then if the Grid contain more than 5 children it hide the firsts children. It work correctly to hide the border but the TextBlock (the child of Border) remain visible.
Any idea where could be the problem? Thanks!
Border TextBorder = new Border();
TextBorder.BorderBrush = new SolidColorBrush(_settings.TextColor);
TextBorder.BorderThickness = new Thickness(0,0,0,2);
TextBorder.Padding = new Thickness(0, 10, 0, 10);
RowDefinition rd = new RowDefinition();
rd.Height = GridLength.Auto;
myGrid.RowDefinitions.Add(rd);
TextBlock uc = new TextBlock();
uc.Text = "Test";
TextBorder.Child = uc;
Grid.SetRow(TextBorder, myGrid.RowDefinitions.Count -1);
myGrid.Children.Add(TextBorder);
if (myGrid.Children.Count > 5)
{
Border border = (Border)myGrid.Children[myGrid.Children.Count - 6];
border.Visibility = Visibility.Hidden;
border.Child.Visibility = Visibility.Hidden;
}
Update
The code work correctly. The problem was in OnRender event of the TextBlock that draw the text with some graphic effect. I though that if the control is invisible OnRender should not be raised but it seams that it is raised also when the control is invisible. I have not found a way to prevent OnRender to be raised, nor ClipToBound nor Invisible work. So I give up with this approach and I just check in OnRender if the TextBlock is in the visible area of the container.
first of all, I think what you do is something you should not do!
But here is how you can do it (btw this assumes you only add Borders to your grid):
if (myGrid.Children.Count > 5)
{
(myGrid.Children[myGrid.Children.Count - 6] as Border).Visibility = Visibility.Hidden;
}
also i recommend to remove not to hide the child as it will otherwise stay in existance without any point
myGrid.Children.Remove(myGrid.Children[0]);

How to show more that one image in the radtreeview item (wpf - telerik )

I am adding image to the radtreeviewitem from resources programatically using the below code.
"/myAssembley;component/Resources/image1.png"
and the image is displaying successfully. Now i need to add another image which needs to be displayed next to the first image in the radtreeviewitem.
how to achieve it.?
Like the below image i need my treeviewitem to display a folder icon and a red square icon in a single treeview item.
If you do not have data binding and you are using RadTreeViewItems directly you can add the additional image in the Header of the item. For example:
var stackPanel = new StackPanel() { Orientation = System.Windows.Controls.Orientation.Horizontal };
var image1 = new Image() { Source = image1Path };
var image2 = new Image() { Source = image2Path };
var textBlock = new TextBlock() { Text = itemHeader };
stackPanel.Children.Add(image1);
stackPanel.Children.Add(image2);
stackPanel.Children.Add(textBlock);
var treeViewItem = new RadTreeViewItem()
{
Header = stackPanel,
};
It Works.
The proper way would be to create a DataTemplate with a grid or horizontal stackpanel. Put two images inside and in your model two Image Sources that you can bind too. Telerik doesn't have the best track record using the MVVM pattern, but the TreeView control is pretty decent with binding. If you need help with the model and the datatemplate, post some of your code here and we can work on it.

Wrap text inside in a ScrollViewer windows phone 8.0

I have some text in a ScrollViewer control written in c# code, how do I wrap the text? Any solution?
Initialy I have a pop-up, inside this a stack panel and added
ScrollViewer sv = new ScrollViewer();
In this scroll viewer i put some text.
string values[]= new string[]; //(example)
sv.Content = values[1];
When I open pop-up, if text length is more than screen size, he need to show scroll bar.
sv.TextWrapping = TextWrapping.Wrap;
is possible or something another way?
All items ( scroll viewer, text ) I puts behind view, not in xaml (view) because my content are dynamic.
For you to be able to scroll anything inside a scrollviewer, you have to stop the scrollviewer from growing with the child element.
If this happens dynamically, give the scrollviewer a certain height and the text inside it will be scrollable
Create a TextBlock, assign the text to this TextBlock and add this TextBlock as the content of the ScrollViewer
var sv = new ScrollViewer();
var tb = new TextBlock
{
Text = //your text,
TextWrapping = TextWrapping.Wrap
}
sv.Content = tb;

How to add a scrollViewer in to a stack panel for a text block?

I try to add an scroll Viewer for a text block who was created from behind c#, text block was added to a stack panel stackPanel.Children.Add(text block). I want to do that in Windows Phone 8.0.
When make something like that:
StackPanel stackPanel = new StackPanel();
ScrollViewer sv = new ScrollViewer();
sv.Content = stackPanel;
I receive:
ExceptionObject = {"Value does not fall within the expected range."}.
One solution to solve that exception?
ScrollViewer calculates it's scrollbars based on dimensions of child controls.
If your TextBlock has Height property set, remove it and ScrollBars should work as expected.
Also you should set:
sv.Content = yourTextBlock;
With the following code (where Content is Grid):
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var textBlock = new TextBlock() { Text = "hello" };
var stackPanel = new StackPanel();
stackPanel.Children.Add(textBlock);
var sv = new ScrollViewer { Content = stackPanel };
this.Content.Children.Add(sv);
}
I get the desired output:
So I tried to reproduce your error. I get the same exception if TextBlock is null. So maybe your code which creates the TextBlock has some issues? Here's an example:
TextBlock text = null;
var stackPanel = new StackPanel();
stackPanel.Children.Add(text);
var sv = new ScrollViewer { Content = stackPanel };
this.Content.Children.Add(sv);
Will result in:

User Controlled Divider in Dockpanel

I'm using a Dockpanel via C# & WPF to display 2 user controls
The Left UserControl is a Datagrid with Filters (Called Filter)
The Right UserControl is a Custom Form That will change depending on what type of Data the user is viewing.
I'm setting the Dockpanel via this code
private void SetMasterDock(UIElement MyFilter, UIElement NewViewer)
{
MasterDock.Children.Clear();
DockPanel.SetDock(MyFilter, Dock.Left);
DockPanel.SetDock(NewViewer, Dock.Right);
MasterDock.Children.Add(MyFilter);
MasterDock.Children.Add(NewViewer);
}
All the above works as coded.
Now the Change I'm looking for (If possible)
I'd like to know what / how to enable the User to be able to Adjust the Scaling of the two Usercontrols. so if they wish to see More or less of one side or the other, they can just Click & Slide a Divider bar so they can adjust their view to their personal preferences.
ETA: New Code
MasterDock.Children.Clear();
Grid SplittableGrid = new Grid();
GridSplitter MovableDevider = new GridSplitter(); MovableDevider.Background = Brushes.Blue; MovableDevider.HorizontalAlignment = HorizontalAlignment.Right; MovableDevider.VerticalAlignment = VerticalAlignment.Stretch; MovableDevider.Width = 5;
ColumnDefinition LeftDefinition = new ColumnDefinition(); LeftDefinition.Width = new GridLength(200);
ColumnDefinition RightDefinition = new ColumnDefinition(); RightDefinition.Width = new GridLength(1,GridUnitType.Star);
SplittableGrid.ColumnDefinitions.Add(LeftDefinition);
SplittableGrid.ColumnDefinitions.Add(RightDefinition);
Grid.SetColumn(MyFilter, 0);
Grid.SetColumn(MovableDevider, 0);
Grid.SetColumn(NewViewer, 1);
SplittableGrid.Children.Add(MyFilter);
SplittableGrid.Children.Add(MovableDevider);
SplittableGrid.Children.Add(NewViewer);
DockPanel.SetDock(SplittableGrid, Dock.Left);
MasterDock.Children.Add(SplittableGrid);
in winforms the control you are looking for is the splitcontainer. However in WPF this is done using grid + gridSplitter. both of those controls are in the default toolbox.

Categories

Resources