I have made a list that is a property amnd then given that list values and this is still not working to place in a listview as a itemsource i have no idea how to fix this and get results is there anyone that can show me what im doing wrong here? i am placing the data context in the MainWindow
XAML
<Window x:Class="CRM.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:local="clr-namespace:CRM"
mc:Ignorable="d"
Title="MainWindow" Height="1080" Width="1920">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="0*"/>
</Grid.ColumnDefinitions>
<ListView ItemsSource="{Binding tickets}" Margin="0,10,1075,0" MaxWidth="990"/>
</Grid>
</Window>
MainViewModel:
using API.Objects;
using API.ViewModels;
using System;
using System.Collections.Generic;
using System.Text;
namespace API.ViewModels
{
public class MainViewModel : BaseViewModel
{
int Counter{ get; set; }
List<TicketO> tickets { get; set;}
public MainViewModel()
{
TicketO ticket = new TicketO("Jens", "Svensson", "jenson1234#live.se", "0767942768", "This is working but my box is not", 500);
tickets.Add(ticket);
}
}
}
MainWindow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using API.ViewModels;
namespace CRM
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
}
Make tickets public, DataContext = this; is missing, refer below link.
How can I bind a List as ItemSource to ListView in XAML?
Related
I've used CaliburnMicro to implement data binding for my data grid:
<Window x:Class="TicketDemo.Views.ShellView"
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:local="clr-namespace:TicketDemo.Views"
mc:Ignorable="d" FontSize="20"
Title="ShellView" Height="450" Width="800"
Closing="{Binding Path=OnClose}">
<Grid>
<DataGrid x:Name="Tickets" AlternatingRowBackground="LightGray" CanUserAddRows="True"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding Path=Title}"/>
<DataGridTextColumn Header="Content" Binding="{Binding Path=Content}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
I tried to use the same data binding to link a method called OnClose() to the Closing event.
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using TicketDemo.Models;
namespace TicketDemo.ViewModels
{
class ShellViewModel : Screen
{
public BindableCollection<TicketModel> Tickets { get; set; }
public ShellViewModel()
{
Tickets = SQLiteDataAccess.LoadTickets();
}
public void OnClose(object sender, EventArgs e)
{
MessageBox.Show("Window Closing");
}
}
}
When I debug the program, I get this message:
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace TicketDemo.Views
{
/// <summary>
/// Interaction logic for ShellView.xaml
/// </summary>
public partial class ShellView : Window
{
public ShellView()
{
InitializeComponent();
MessageBox.Show("Hello");
}
}
}
System.Windows.Markup.XamlParseException: ''Provide value on
'System.Windows.Data.Binding' threw an exception.' Line number '9' and
line position '9'.'
InvalidCastException: Unable to cast object of type
'System.Reflection.RuntimeEventInfo' to type
'System.Reflection.MethodInfo'.
The SO post answering a question about this exception relates to buttons, but this is an event, so I don't think it's helpful to resolve my problem.
if you are using caliburn, the event is declared like this:
replace Closing="{Binding Path=OnClose}"
by
cal:Message.Attach="[Event Closing] = [Action OnClose]"
(dont forget to add xmlns:cal="http://www.caliburnproject.org")
in ViewModel you write:
public void OnClose()
{
MessageBox.Show("Window Closing");
}
i'm trying to learn WPF and on top of that the MVVM style of doing things.
I have a simple practice app in which i would like to display codes in a combo box.
My Code
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SteamCodes
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<Codes> codes;
public MainWindow()
{
InitializeComponent();
codes = new ObservableCollection<Codes>()
{
new Codes() {CodeID = "1", Code="CODETEXT"}
};
steamCode.ItemsSource = codes.ToString();
}
}
public class Codes
{
public string CodeID { get; set; }
public string Code { get; set; }
}
}
My XAML
<Window x:Class="SteamCodes.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:local="clr-namespace:SteamCodes"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox x:Name="steamCode" ItemsSource="{Binding Source = Codes}" HorizontalAlignment="Left" Height="43" Margin="122,37,0,0" VerticalAlignment="Top" Width="259"/>
</Grid>
</Window>
At the moment my Combo box is Pulling through as each option in the ComboBox is a letter from the line 'System.Collections.Objectmodel.ObservableCollection`1[SteamCodes.Codes]'
Everyone of those letters is a different drop down option in the combo box.
Any Ideas where i have gone wrong.
Your ComboBox ItemSource must be a collection of items, not a string:
steamCode.ItemsSource = codes;
You also have to specify which property of your item must be considered as value to be shown in combobox by setting DisplayMemberPath property:
steamCode.DisplayMemberPath = "Code";
To specify which property of bound objects will be used as actual selected value you have to use SelectedValuePath property:
steamCode.SelectedValuePath = "CodeID";
The MVVM approach is this:
public class ViewModel
{
public ObservableCollection<Codes> Codes { get; }
= new ObservableCollection<Codes>();
}
The MainWindow constructor:
public MainWindow()
{
InitializeComponent();
var viewModel = new ViewModel();
viewModel.Codes.Add(new Codes { CodeID = "1", Code = "CODETEXT" });
DataContext = viewModel;
}
The XAML:
<ComboBox ItemsSource="{Binding Codes}" DisplayMemberPath="Code" .../>
I have a simple wpf app, that I want to try to use ItemsSource binding from xaml. When I click the button. it should be updated also in the UI, but it doesn't.
Why doesn't it work?
Xaml code:
<Window x:Class="SendRawEthernetPacketsGUI.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<ComboBox HorizontalAlignment="Left" Margin="76,65,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding test}"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="90,171,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
C# code:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace SendRawEthernetPacketsGUI
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public ObservableCollection<string> test = new ObservableCollection<string>();
public Window1()
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
test.Add("fdfddf");
}
}
}
It feels kinda stupid to even ask this, but even looking in google didn't help me. So can you?
Your binding never worked in the first place. You can only bind to properties not fields.
Try this instead:
public ObservableCollection<string> test { get; set; }
public Window1()
{
Test = new ObservableCollection<string>();
}
Or if you want some tricky C# 6 magic:
public ObservableCollection<string> test => new ObservableCollection<string>();
This is a function bodied-member and compiles to a read-only property that is initialized to the new ObservableCollection
Caveats/Design Errors:
Note that in both cases you aren't using INotifyPropertyChanged, so wholesale assignments to the collection won't be picked up by the UI. You should also be using PascalCase for your public properties, and using a proper view model instead of binding to the code-behind.
This question already has answers here:
Passing parameters between viewmodels
(2 answers)
Closed 8 years ago.
Goal:
When you click one of the rows in the lstView_bbb (in Test.xaml), the return value shall be return to the class MainWindow (MainWindow.xaml). And then, the Test.xaml shall be closed.
Problem:
I tried finding a solution that data of "_a" from row shall be transferred to the MainWIndow but it didn't go so well due to performance issue. I want to make it more efficient.
Information:
*I'm using WPF with VS 2013
*The return value is "_a" to the class MainWIndow.xaml
<Window x:Class="WpfApplication1.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>
<Button x:Name="btn_test" Content="Test" HorizontalAlignment="Left" Margin="348,240,0,0" VerticalAlignment="Top" Width="75" Click="btn_test_Click"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private List<aaa> _myList_aaa;
private void btn_test_Click(object sender, RoutedEventArgs e)
{
_myList_aaa = new List<aaa>();
for (int a = 1; a <= 5; a++)
{
aaa myaaa = new aaa();
myaaa._a = a;
myaaa._city = "New Yor";
myaaa._name = "jj jj jj";
_myList_aaa.Add(myaaa);
}
Test myTest = new Test(_myList_aaa);
myTest.ShowDialog();
}
}
public class aaa
{
public int _a { get; set; }
public string _name { get; set; }
public string _city { get; set; }
}
}
---------------------------------------
<Window x:Class="WpfApplication1.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="300" Width="300">
<Grid Background="#FFE5E5E5">
<ListView x:Name="lstView_bbb" SelectionMode="Single" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="111" Margin="35,67,0,0" VerticalAlignment="Top" Width="222">
<ListView.View>
<GridView>
<GridViewColumn Header="name" Width="auto" DisplayMemberBinding="{Binding Path=_name}" TextBlock.TextAlignment="Left" />
<GridViewColumn Header="city" Width="auto" DisplayMemberBinding="{Binding Path=_city}" TextBlock.TextAlignment="Center" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Test.xaml
/// </summary>
public partial class Test : Window
{
public Test(IList<aaa> pAAA)
{
InitializeComponent();
lstView_bbb.DataContext = pAAA;
}
}
}
The easiest way is to create a property in Test:
class Test
{
public aaa SelectedItem
{
get
{
return lstView_bbb.SelectedItems[0] as aaa;
}
}
The best way is to use a ViewModel. Assign it to Test and use the same ViewModel in you other form to get the selected value.
Read the related MSDN article.
I was wondering is it possible to create a panel-like control in wpf, and another wpf custom control that will be the child of this panel, all in WinForms application. And then, would it be possible to set the custom control's size in float?
Also would I be able to rotate that custom control?
Thanks in advance
You will need to use the System.Windows.Forms.Integration NameSpace, more specifically the ElementHost Control, that will allow you to embed a WPF Control in Winforms.
This is a quick and simple demonstration of changing the WPF UserControls Child UserControls Size and Location. I will leave the animation to you.
Main Winform Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
ElementHost host;
WpfControlLibrary1.UserControl1 uc;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
host = new ElementHost();
host.Dock = DockStyle.Top;
uc = new WpfControlLibrary1.UserControl1();
host.Child = uc;
this.Controls.Add(host);
}
private void button1_Click(object sender, EventArgs e)
{
uc.SetChildLocation = new System.Windows.Point(uc.SetChildLocation.X + 25.5, uc.SetChildLocation.Y + 10.2);
}
private void button2_Click(object sender, EventArgs e)
{
uc.SetChildSize = new System.Windows.Size(uc.SetChildSize.Width + .25, uc.SetChildSize.Height + .25);
}
}
}
UserControl1.xaml
<UserControl x:Class="WpfControlLibrary1.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" Background="Red" xmlns:my="clr-namespace:WpfControlLibrary1">
<Canvas>
<my:UserControl2 HorizontalAlignment="Center" x:Name="userControl21" VerticalAlignment="Center" Canvas.Left="0" Canvas.Top="0" />
</Canvas>
</UserControl>
UserControl1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfControlLibrary1
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public Point SetChildLocation
{
get
{
return new Point(Canvas.GetLeft(userControl21), Canvas.GetTop(userControl21));
}
set
{
Canvas.SetLeft(userControl21, value.X);
Canvas.SetTop(userControl21, value.Y);
}
}
public Size SetChildSize
{
get
{
return new Size(userControl21.ActualWidth, userControl21.ActualHeight);
}
set
{
userControl21.Width = value.Width;
userControl21.Height = value.Height;
}
}
}
}
UserControl2.xaml
<UserControl x:Class="WpfControlLibrary1.UserControl2"
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" SizeChanged="UserControl_SizeChanged">
<Grid>
<Rectangle x:Name="rect" Fill="Blue" Height="40" Width="120"></Rectangle>
</Grid>
</UserControl>
UserControl2.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfControlLibrary1
{
/// <summary>
/// Interaction logic for UserControl2.xaml
/// </summary>
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
rect.Width = e.NewSize.Width;
rect.Height = e.NewSize.Height;
}
}
}