I'm very new to WPF and I'm attempting to create a treelist navigation within each navbar group. Because the number of navbar groups and treelists are dynamic I have to make them in code rather than them be pre-defined in XAML.
I have tested the following so far which is meant to define the navbar group's content rather than use the default item
private void CreateGroup2(NavBarControl navBar)
{
NavBarGroup group2 = new NavBarGroup();
group2.Header = "Custom Content";
//Specify that the group's content should be defined via the Content property
group2.DisplaySource = DisplaySource.Content;
TreeListControl tree = new TreeListControl();
tree.ItemsSource = TreeList_DataBinding.Stuff.GetStuff();
group2.Content = tree;
navBar.Groups.Add(group2);
}
This gives an Exception: Grid.InfiniteGridSizeException: By default, an infinite grid height is not allowed since all grid rows will be rendered and hence the grid will work very slowly. To fix this issue, you should place the grid into a container that will give a finite height to the grid, or you should manually specify the grid's Height or MaxHeight. Note that you can also avoid this exception by setting the TreeListControl.AllowInfiniteGridSize static property to True, but in that case the grid will run slowly.
I'm a little confused as I'm not using a grid? Can anyone give any pointers what's wrong and how I can add a treview under each navbar group?
Thank You
It feels a bit wrong answering my own question but I managed to get it working using the following
private void CreateGroup2(NavBarGroup navBarGroup)
{
System.Windows.Controls.TreeView treeview = new System.Windows.Controls.TreeView();
TreeViewItem nod = new TreeViewItem();
nod.Header = "Tree Node1";
treeview.Items.Add(nod);
TreeViewItem nod1 = new TreeViewItem();
nod1.Header = "Tree Node2";
treeview.Items.Add(nod1);
TreeViewItem nod2 = new TreeViewItem();
nod2.Header = "Tree Node3";
nod1.Items.Add(nod2);
//StackPanel stcPnl = new StackPanel(); /optiona
//stcPnl.Children.Add(treeview);
//navBarGroup.Content = stcPnl;
navBarGroup.Content = treeview;
navBarGroup.DisplaySource = DisplaySource.Content;
}
Related
I want to send an element behind all of the elements.
I tried to use Z-Index but it did not work because I need to send it behind all of the elements and I don't know how many are they(elements can increase by user);
pls help.
I'm assuming that you want to do this in code, that you want to place a new element (in my example, a Label) "behind".
The way to do this is to replace the current Content of your control/window with a Grid, and then place your element in it together with the current content.
void PlaceLabelBehindEverything()
{
var newLabel = new Label() { Content = "My Label that is behind everything" };
var currentContent = this.Content; // assuming this method is in the control/window
var grid = new Grid();
this.Content = grid;
grid.Children.Add(newLabel);
grid.Children.Add(currentContent);
{
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.
I am using WPF and created a Window with informations about the computer.
It stores informations like Network connectivity, IP's, Subnet masks, Network devices and other stuff.
To track changes in the system I want to add an timer on an object to refresh itself. I don't want to refresh the hole form because I had HttpWebRequests in it and it will freeze the programm for a few seconds. It should be easier to see changes and to highlight them.
For example:
StComputerInf.Children.Add(new Label { Content = "2. Domain: \t\t" + System.Environment.UserDomainName });
I want to add here an timer to refresh itself.
And for every TreeViewItem in a TreeView:
public TreeView CreatTVConnection()
{
List<CAdapter> LAdapter = new List<CAdapter>();
List<TreeViewItem> lConnectedDevices = new List<TreeViewItem>();
List<TreeViewItem> lDisconnectedDevices = new List<TreeViewItem>();
LAdapter = ReadAdapter();
TreeView tv_Adapter = new TreeView();
tv_Adapter.Name = "Adapter";
tv_Adapter.Background = System.Windows.Media.Brushes.Transparent;
tv_Adapter.BorderThickness = new Thickness(0);
TreeViewItem Connected = new TreeViewItem();
TreeViewItem Disconnected = new TreeViewItem();
lConnectedDevices = LoadTV(true, LAdapter);
if (lConnectedDevices.Count > 0)
{
Connected.Header = "Connected:";
Connected.FontWeight = FontWeights.Bold;
Connected.Foreground = System.Windows.Media.Brushes.Green;
Connected.Name = "Connected";
foreach (TreeViewItem tvi in lConnectedDevices)
{
tvi.FontWeight = FontWeights.Normal;
Connected.Items.Add(tvi);
}
}
....
And is there a way to see if an object have changed? So I can highlight the affected object?
Use a factory to create your objects.
So for the label example you'd use something like
LabelFactory.Create(any useful parameters here) and as part of that method you can include a timer etc.
Also, look into using async/await to update your forms as an easier way to update them without freezing the forms. Once you are comfortable with the pattern you should be able to remove the dependency on timers.
I have a question - I was coding happily a quick little feature to an app, which was a simple comparison output window.
Basically, user clicks a button and I generate a window with a datagrid of two columns of data.
It's all great and a five minutes code living inside one method with no unnecessary reference to anything else. The only problem I encountered was when I wanted to add a TopMost checkbox to this window.
How do I bind the IsChecked property of the box to the TopMost property of the window?
var checkbox = new CheckBox();
checkbox.Name = "cb";
checkbox.Content = "Top most";
var grid = new DataGrid();
grid.ItemsSource = wcList;
grid.Margin = new Thickness(5);
var panel = new StackPanel();
panel.Children.Add(checkbox);
panel.Children.Add(grid);
var win = new Window();
//Binding b = new Binding("cb");
//b.Mode = BindingMode.TwoWay;
//b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
//win.SetBinding(Window.TopmostProperty, b);
win.Title = "WordCount comparison";
win.Content = panel;
win.SizeToContent = SizeToContent.WidthAndHeight;
win.Icon = this.Icon;
win.Show();
This was supposed to be a 5-minutes one-off method, which is why I don't want to go as far as adding any xaml or properties into the code.
Cheers
Bartek
The other way around as you tried (in the commented code):
checkbox.SetBinding(CheckBox.IsCheckedProperty, new Binding("Topmost") { Source = win });
just after you instantiated your win variable.
I'm writing a simple tic tac toe game for school. The assignment is in C++, but the teacher has given me permission to use C# and WPF as a challenge. I've gotten all the game logic finished and the form mostly complete, but I've run into a wall. I'm currently using a Label to indicate who's turn it is, and I want to change it when a player makes a valid move. According to Applications = Code + Markup, I should be able to use the FindName method of the Window class. However, it keeps returning null. Here's the code:
public TicTacToeGame()
{
Title = "TicTacToe";
SizeToContent = SizeToContent.WidthAndHeight;
ResizeMode = ResizeMode.NoResize;
UniformGrid playingField = new UniformGrid();
playingField.Width = 300;
playingField.Height = 300;
playingField.Margin = new Thickness(20);
Label statusDisplay = new Label();
statusDisplay.Content = "X goes first";
statusDisplay.FontSize = 24;
statusDisplay.Name = "StatusDisplay"; // This is the name of the control
statusDisplay.HorizontalAlignment = HorizontalAlignment.Center;
statusDisplay.Margin = new Thickness(20);
StackPanel layout = new StackPanel();
layout.Children.Add(playingField);
layout.Children.Add(statusDisplay);
Content = layout;
for (int i = 0; i < 9; i++)
{
Button currentButton = new Button();
currentButton.Name = "Space" + i.ToString();
currentButton.FontSize = 32;
currentButton.Click += OnPlayLocationClick;
playingField.Children.Add(currentButton);
}
game = new TicTacToe.GameCore();
}
void OnPlayLocationClick(object sender, RoutedEventArgs args)
{
Button clickedButton = args.Source as Button;
int iButtonNumber = Int32.Parse(clickedButton.Name.Substring(5,1));
int iXPosition = iButtonNumber % 3,
iYPosition = iButtonNumber / 3;
if (game.MoveIsValid(iXPosition, iYPosition) &&
game.Status() == TicTacToe.GameCore.GameStatus.StillGoing)
{
clickedButton.Content =
game.getCurrentPlayer() == TicTacToe.GameCore.Player.X ? "X" : "O";
game.MakeMoveAndChangeTurns(iXPosition, iYPosition);
// And this is where I'm getting it so I can use it.
Label statusDisplay = FindName("StatusDisplay") as Label;
statusDisplay.Content = "It is " +
(game.getCurrentPlayer() == TicTacToe.GameCore.Player.X ? "X" : "O") +
"'s turn";
}
}
What's going on here? I'm using the same name in both places, but FindName can't find it. I've tried using Snoop to see the hierarchy, but the form doesn't show up in the list of applications to choose from. I searched on StackOverflow and found I should be able to use VisualTreeHelper class, but I don't understand how to use it.
Any ideas?
FindName operates on the XAML namescope of the calling control. In your case, since the control is created entirely within code, that XAML namescope is empty -- and that's why FindName fails. See this page:
Any additions to the element tree after initial loading and processing must call the appropriate implementation of RegisterName for the class that defines the XAML namescope. Otherwise, the added object cannot be referenced by name through methods such as FindName. Merely setting a Name property (or x:Name Attribute) does not register that name into any XAML namescope.
The easiest way to fix your problem is to store a reference to your StatusDisplay label in the class as a private member. Or, if you want to learn how to use the VisualTreeHelper class, there's a code snippet at the bottom of this page that walks the visual tree to find the matching element.
(Edited: Of course, it's less work to call RegisterName than to use the VisualTreeHelper, if you don't want to store a reference to the label.)
I'd recommend reading the first link in its entirety if you plan on using WPF/Silverlight in any depth. Useful information to have.
You have to create a new NameScope for your window:
NameScope.SetNameScope(this, new NameScope());
Then you register name of your label with the window:
RegisterName(statusDisplay.Name, statusDisplay);
So this seems to be all you need to do to make FindName() work.