Chapter 10 of "WPF 4 Unleashed" by Adam Nathan includes this XAML example of controlling ListBox scrolling behaviour:
<Window x:Class="NathanControllingScrollingBehavior.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.CanContentScroll="False"
ScrollViewer.IsDeferredScrollingEnabled="True">
...
</ListBox>
</Window>
There isn't an equivalent C# example in the book. I did some research and found a few roundabout ways to do this. See this SO question for two approaches which worked for me. These approaches however, seem kind of hackish. Adjusting these properties is quite simple in XAML, but awkward from C#.
Is this simply an area of WPF which was meant to be used only from XAML and not C#? Can anyone explain the disparity in ease of use of these properties between XAML/C#? Is it just an oversight of the WPF team?
You can do it this way.
ListBox listBox1 = new ListBox();
listBox1.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty,
ScrollBarVisibility.Disabled);
listBox1.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty,
ScrollBarVisibility.Disabled);
listBox1.SetValue(ScrollViewer.CanContentScrollProperty, false);
listBox1.SetValue(ScrollViewer.IsDeferredScrollingEnabledProperty, true);
Related
I spent the night trying to solve this problem but couldn't unfortunately. Please show mercy, since I am not proficient in C#.
My MainWindow.xaml is pretty much from the MS support pages:
<Window x:Class="TrayApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:r="clr-namespace:System;assembly=mscorlib"
xmlns:local="using:clr-namespace:TrayApp"
Title="MainWindow" Height="825" Width="500"
Deactivated="Window_Deactivated"
ShowInTaskbar="False"
WindowStyle="None">
<DockPanel>
<wv2:WebView2 Name="webView"
Source = "https://website.com" />
</DockPanel>
</Window>
My goal is, whenever the webview2 control is launched the URL has to be dynamically changed to https://website.com/?param=hostname
I know that its possible to get hostname of a PC via Environment.GetEnvironmentVariable("COMPUTERNAME") however, I tried very many variations data bindings and other tricks, but nothing seems to work. Hopefully someone someone can help.
UPDATE:
Thanks to Klaus Gütters suggestion, it made click in my head and it works as intended now!
My MainWindow.xaml.cs looks like this:
public MainWindow()
{
InitializeComponent();
this.Hide_Window();
string host = Environment.GetEnvironmentVariable("COMPUTERNAME");
webView.Source = new Uri("https://website.com/?param=" + host);
And my MainWindow.xaml like this:
<wv2:WebView2 x:Name="webView"
Source = ""
I made a WPF control in a library project and would like to extend it with a new one.
<UserControl x:Class="Genesyslab.Desktop.Modules.ExtensionUtils85.GUI.EmbeddingUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</UserControl>
I have tried to extend it like this:
<src:EmbeddingUserControl x:Class="Interaxalab.Desktop.Modules.PrototipoCable.CustomViews.InteractionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="Genesyslab.Desktop.Modules.ExtensionUtils85.GUI"
Name="InteractionWorksheetView" Height="321.613" Width="471.396"
>
<Grid>
<WindowsFormsHost x:Name="windowsFormsHost1" HorizontalAlignment="Left" Height="284" VerticalAlignment="Top" Width="471"/>
</Grid>
</src:EmbeddingUserControl>
However, I get an error message saying that the name "EmbeddingUserControl" does not exist in namespace "Genesyslab.Desktop.Modules.ExtensionUtils85.GUI".
The name clearly does exist, since the xaml.cs can find it, but for some reason the xaml cannot.
Any help would be appreciated.
Long story short - you cannot inherit control with xaml by another control with xaml (and does it makes sense even to do so?). In your case, EmbeddingUserControl does not contain any visual tree elements (just empty grid), so you can just do:
public class EmbeddingUserControl : UserControl {
// some methods, properties of your control
}
Then you can inherit exactly like you do already in your question (don't forget to inherit from EmbeddingUserControl both in xaml file and in code-behind file).
You can also inherit from user control with xaml, if your inherited control does not have xaml itself (so you can add\override logic).
If you need to inherit some visual stuctures - you have to switch from inheritance to composition. That is: your base control provides some placeholders where other controls may be placed, or even better allows to provide templates to format data items (like for example ItemsControl and ItemTemplate property). Then you just fill those placeholders with other controls if necessary.
I am developing a GUI application in Visual Studio with WPF. It seems that I've added too many things to my window and VS lags horrifically. It's also a bit difficult to work with too many elements on the screen.
A lot of my functionality is grouped together in their own tabs or panels. Isn't there a way to just work on one panel at a time, and then tell VS to include that in so and so part of the main window?
Break your functionality into UserControls, each with a specific purpose. Then group them together into more complex combinations of controls and then utlimately your application window.
I would investigate a UI pattern like Model-View-ViewModel or MVVM which fits nicely with WPF.
// MainWindow.xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Frame Name="EmbeddedView" Source="EmbeddedView.xaml"/>
</Grid>
</Window>
// EmbeddedView.xaml
<UserControl x:Class="EmbeddedView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
Embedded Text
</UserControl>
Actually, more than just UserControls, you need the concept of Views and ViewModels - see Josh Smith's seminal article in MSDN Magazine:
WPF Apps With The Model-View-ViewModel Design Pattern
Eventually, you'll want to move on to something like the following, but understand Views and ViewModels first:
Caliburn.Micro: Screens, Conductors, and Composition
I am new at MVVM. Currently all my code is written in .cs file which linked to XAML. I want switch to MVVM but experiencing difficulties. I will try to explain why:
I have many different Chart controls and input data specified in .cs file in the way that I am accessing Chart object directly and using it's properties programaticaly to add points for my chart.
Example:
foreach (var group in qcv.Groups)
{
AreaSeries areaSeries = new AreaSeries();
areaSeries.CombineMode = Telerik.Charting.ChartSeriesCombineMode.Stack;
areaSeries.ValueBinding = new PropertyNameDataPointBinding("Rev");
areaSeries.CategoryBinding = new PropertyNameDataPointBinding("Date");
areaSeries.ItemsSource = group as IEnumerable;
RadChart1.Series.Add(areaSeries);
}
But as long as I switch to MVVM RadChart1 objects gets inaccessible in ViewModel file. How can I make it visible in ViewModel class or maybe you can suggest better approach how I can get that object and provide input for my chart without changing my code behind?
My XAML File:
<UserControl x:Class="FrontEnd.RevenueChart"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FrontEnd"
mc:Ignorable="d" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" HorizontalAlignment="Stretch" >
<UserControl.DataContext>
<local:RevenueChartViewModel/>
</UserControl.DataContext>
<Grid>
<telerik:RadCartesianChart HorizontalAlignment="Stretch" x:Name="RadChart1" Palette="Metro" Zoom="10,1">
<telerik:RadCartesianChart.HorizontalAxis>
<telerik:CategoricalAxis/>
</telerik:RadCartesianChart.HorizontalAxis>
<telerik:RadCartesianChart.VerticalAxis>
<telerik:LinearAxis/>
</telerik:RadCartesianChart.VerticalAxis>
<telerik:RadCartesianChart.Behaviors>
<telerik:ChartPanAndZoomBehavior ZoomMode="Both">
</telerik:ChartPanAndZoomBehavior>
</telerik:RadCartesianChart.Behaviors>
</telerik:RadCartesianChart>
</Grid>
</UserControl>
Accessing view controls from the viewmodel is a big no-no in MVVM land. You have to invert your thinking: instead of adding stuff to Series, bind Series to stuff. Use SeriesMappings to get the chart control to convert your groups to series. Here's a bit of off-the-cuff code to get you started:
<telerik:RadCartesianChart HorizontalAlignment="Stretch"
Palette="Metro" Zoom="10,1"
Series="{Binding Groups}"><!-- <=== this is the important part -->
<telerik:RadChart.SeriesMappings>
<telerikCharting:SeriesMapping LegendLabel="Product Sales">
<telerikCharting:SeriesMapping.SeriesDefinition>
<telerikCharting:AreaSeriesDefinition/>
</telerikCharting:SeriesMapping.SeriesDefinition>
<telerikCharting:SeriesMapping.ItemMappings>
<telerikCharting:ItemMapping DataPointMember="XCategory" FieldName="Date"/>
<telerikCharting:ItemMapping DataPointMember="YValue" FieldName="Rev"/>
</telerikCharting:SeriesMapping.ItemMappings>
</telerikCharting:SeriesMapping>
</telerik:RadChart.SeriesMappings>
...
I'm new to WPF programming and decided to give it a shot by trying out some ribbon control libraries.
The library that looks best for now is the Microsoft RibbonControlsLibrary. You can get it on the ribbon licensing page.
So far I've started a new project, added the control to the windows, but them I'm stuck: This is the code so far:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="808" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:my1="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary" WindowStyle="SingleBorderWindow">
<Grid>
<my1:Ribbon HorizontalAlignment="Left" Name="ribbon1" VerticalAlignment="Top" Height="165" Width="786" ShowQuickAccessToolbarOnTop="False" WindowIconVisibility="Visible" DataContext="{Binding}" Margin="0,-20,0,0">
<my1:Ribbon.ApplicationMenu>
<my1:RibbonApplicationMenu Visibility="Hidden" IsEnabled="True" />
</my1:Ribbon.ApplicationMenu>
<my1:RibbonTab Label="Tab1" Name="rtab1" >
</my1:RibbonTab>
<my1:RibbonTab Label="tab2" Name="rtab2"/>
</my1:Ribbon>
</Grid>
</Window>
Questions:
1) Where can I find samples for this ribbon control? I've tried googling, but came up with nothing useful.
2) How to add items to specific ribbon tabs? I'm lost in all these properties in the property grid. So far I havent found a designer for that purpose.
3) How can I switch the designer to show me what icons/button/... I placed on TabPage2?
(FYI: The fluent ribbon library does not seem to work for me, because I can't get rid of the ApplicationMenu.)
Found a great sample/tutorial:
http://windowsclient.net/downloads/folders/hands-on-labs/entry76491.aspx
The sample provides a manual with explanations and some test projects with step by step instructions to implement the ribbon control.
Though I'm totally new to WPF, I managed to extract necessary classes from the sample to provide a ribon based menu in my program.