How to install these Pie Chart controls in Visual Studio? - wpf

https://www.codeproject.com/Articles/442506/Simple-and-Easy-to-Use-Pie-Chart-Controls-in-WPF
Can't seem to get these working in my WPF project. How do I add the references to get going with this pie chart?

You need to sign in and download the binaries. You should then unpack the zip file and add a reference (Project->Add Reference->Browse->Browse in Visual Studio) to the downloaded and unpacked PieControls.dll file. After you have done this, the following sample code should work.
MainWindow.xaml:
<Window x:Class="WpfApp1.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:pie="clr-namespace:PieControls;assembly=PieControls"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid>
<pie:PieChart x:Name="chart1" Width="260" Height="140" PieWidth="120" PieHeight="120"/>
</StackPanel>
</Window>
MainWindow.xaml.cs:
using PieControls;
...
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<PieSegment> pieCollection = new ObservableCollection<PieSegment>();
pieCollection.Add(new PieSegment { Color = Colors.Green, Value = 5, Name = "Dogs" });
pieCollection.Add(new PieSegment { Color = Colors.Yellow, Value = 12, Name = "Cats" });
pieCollection.Add(new PieSegment { Color = Colors.Red, Value = 20, Name = "Mice" });
pieCollection.Add(new PieSegment { Color = Colors.DarkCyan, Value = 22, Name = "Lizards" });
chart1.Data = pieCollection;
}
}

Related

How to set "ResourceDictionary" as the content of the "Window" in WPF

I am trying to set a ResourceDictionary as the content of window in WPF. technically we can initialize a window with the corresponding view model. but when it comes to the output then the view is coming with the namespace of the view model, not with associated ResourceDictionary for the relevant view model. here is the tried example and output window. Please help me to resolve this problem.
public void ShowDialog(IDocumentPublishDialogViewModel dialogViewModel)
{
if (dialogViewModel is ContinuousJournalPrintViewModel)
{
Window continuousJournalPrintWindow = new ContinuousJournalPrintWindow
{
Owner = Application.Current.MainWindow,
Title = "New Continuous Journal Print",
Content = dialogViewModel
};
continuousJournalPrintWindow.Show();
}
else
{
PrepareCloseAction(dialogViewModel);
if (dialogViewModel is ChooseDraftOrTasksViewModel)
{
GuiDispatcherField.Invoke(() => DialogServiceField.ShowPopupDialog(dialogViewModel, dialogViewModel.CloseAction, () => true, true));
}
else
{
GuiDispatcherField.Invoke(() => DialogServiceField.ShowPopupDialog(dialogViewModel, dialogViewModel.CloseAction, () => true, true, HorizontalAlignment.Stretch, VerticalAlignment.Stretch));
}
}
}
<ResourceDictionary x:Class="DIPSASD.DynamicPublish.Client.Views.Previews.ContinuousJournalPrintView"
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:dc="http://DIPSASD.no/Arenade/UI"
xmlns:local="clr-namespace:DIPSASD.DynamicPublish.Client.ViewModels.Previews"
mc:Ignorable="d">
<DataTemplate DataType="{x:Type local:ContinuousJournalPrintViewModel}">
<dc:TextBlock Text="Team Y-KNOTS"></dc:TextBlock>
</DataTemplate>
[![enter image description here][1]][1]

WPF Binding Return StaticResource as String

I created a custom checkbox using a UserControl with Image and Label inside. I want to swap the Checked and Unchecked images whenever i click it.
So far i tried doing the following
<Image Source="{Binding StateImage}"/>
I have a property named StateImage
public String StateImage
{
get
{
return is_checked?"{StaticResource Checked}":"StaticResource Unchecked";
}
}
My code doesn't work and i ended up doing like this:
public String StateImage
{
get
{
return is_checked?"/Resources/Images/Checked.png":"/Resources/Images/Unchecked.png";
}
}
the is_checked variable is modified under MouseDown Event of the UserControl
Is there an easier way I can call the image without writing the whole path and filename?
You could define the resources as strings in the UserControl:
<UserControl x:Class="WpfApplication1.UserControl1"
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"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<UserControl.Resources>
<s:String x:Key="Checked">pic.png</s:String>
<s:String x:Key="UnChecked"></s:String>
</UserControl.Resources>
<Grid Background="Yellow">
<Image Source="{Binding StateImage}"/>
</Grid>
</UserControl>
private bool is_checked;
public String StateImage
{
get
{
return is_checked ? Resources["Checked"] as string : Resources["UnChecked"] as string;
}
}

How to use Vlc.DotNet in WPF?

I am trying to use VLC in WPF via Vlc.DotNet. I have been successful in getting Vlc.DotNet to work in Winforms, but thus far unsuccessful with WPF.
I get no errors, but I also get no video... just a blank white pane.
Here is my very simple XAML:
<Window x:Class="VLC.Wpf.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" Closing="Window_Closing">
<Grid x:Name="Grid1">
</Grid>
</Window>
And here is the codebehind I use to insert and start the Vlc Wpf control.
public MainWindow()
{
VlcContext.LibVlcDllsPath = CommonStrings.LIBVLC_DLLS_PATH_DEFAULT_VALUE_AMD64;
VlcContext.LibVlcPluginsPath = CommonStrings.PLUGINS_PATH_DEFAULT_VALUE_AMD64;
VlcContext.StartupOptions.IgnoreConfig = true;
VlcContext.Initialize();
InitializeComponent();
var vlcPlayer = new VlcControl();
var media = new LocationMedia("rtsp://admin:12345#192.168.42.200:554/MediaInput/h264");
Grid1.Children.Add(vlcPlayer);
var vlcBinding = new Binding("VideoSource");
vlcBinding.Source = vlcPlayer;
var vImage = new Image();
vImage.SetBinding(Image.SourceProperty, vlcBinding);
var vBrush = new VisualBrush();
vBrush.TileMode = TileMode.None;
vBrush.Stretch = Stretch.Uniform;
vBrush.Visual = vImage;
Grid1.Background = vBrush;
vlcPlayer.Play();
}
Does anyone see anything wrong with this?
Using Vlc 2.1.5 win32
You haven't set vlcPlayer's Media property.
var vlcPlayer = new VlcControl();
var media = new LocationMedia("rtsp://admin:12345#192.168.42.200:554/MediaInput/h264");
vlcPlayer.Media = media; //add this
Btw, you don't need to add vlcPlayer to Grid1.

Dynamically creating buttons in WPF xaml file/xaml.cs file after reading an xml file?

I am new to WPF,
Problem Statement: I have a xml file that gives me the number of items that i need to create,
for each item, i need a button.
If there are 20 items---> on loading the xaml file,
the xml will be read,
count(of number of items) will be read and created.
Is there a way to do this in xaml file?
Here is a simple/quick fix:
Expose a Panel (say StackPanel) in the Xaml and add the new buttons to them as Children on run time...
MainWindow.xaml:
<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" Loaded="Window_Loaded">
<StackPanel x:Name="mainPanel"/>
</Window>
MainWindow.xaml.cs
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var buttonNames = new List<string>();
// Parse the XML, Fill the list..
// Note: You could do it the way you prefer, it is just a sample
foreach (var buttonName in buttonNames)
{
//Create the button
var newButton = new Button(){Name = buttonName};
//Add it to the xaml/stackPanel
this.mainPanel.Children.Add(newButton);
}
}
Solution using Data Binding
MainWindow.xaml:
<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" >
<ItemsControl ItemsSource="{Binding YourCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Window>
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
YourCollection = new List<Button>();
// You could parse your XML and update the collection
// Also implement INotifyPropertyChanged
//Dummy Data for Demo
YourCollection.Add(new Button() { Height = 25, Width = 25 });
YourCollection.Add(new Button() { Height = 25, Width = 25 });
this.DataContext = this;
}
public List<Button> YourCollection { get; set; }

In XAML bind Text to x index of static collection with x being supplied by the y index of another collection?

I have a static collection of strings and would like to display one of these strings from a certain index. That index is supplied at run time, and is an integer value in the second index in another collection.
I can bind to the static collection, but how do I bind the path to the value in the other
collection?
That is, what do I use as a value to the Path argument in the TextBlock binding?
The code here is just for experimenting and is not part of the working code. It is Visual Studio Designer friendly, and if I get the binding right, it will show Wednesday in the designer without running:
<Window x:Class="BindingCollectionIndex.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindingCollectionIndex"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:AnotherClass
x:Key="Foo" />
</Window.Resources>
<Grid>
<TextBlock
Text="{Binding Source={x:Static local:MyStaticCollections.Days},
Path=[**Wrong ... Foo.CollectionOfIntegers[2]**]}" />
</Grid>
The static class:
using System.Collections.Generic;
namespace BindingCollectionIndex
{
static class MyStaticCollections
{
public static List<string> Days =new List<string> { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
}
}
The class that supplies the index value:
using System.Collections.ObjectModel;
namespace BindingCollectionIndex
{
class AnotherClass
{
private ObservableCollection<int> collectionOfIntegers = new ObservableCollection<int>
{
1,2,3,4,5
};
public ObservableCollection<int> CollectionOfIntegers
{
get
{
return collectionOfIntegers;
}
}
}
}
There is nothing added to the xaml's code behind.
Thanks for reading.
David
Have you considered using a Dictionary?
class DaysViewModel
{
public IDictionary<int,string> Days { get; set; }
public DaysViewModel()
{
Days = new Dictionary<int, string>
{
{1,"Monday"},
{2,"Tuesday"},
{3,"Wednesday"},
{4,"Thursday"},
{5,"Friday"}
};
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:DaysViewModel x:Key="Week" />
</Window.Resources>
<Grid>
<TextBox Text="{Binding Source={StaticResource Week},Path=Days[2]}" />
</Grid>

Categories

Resources