I have a grid of one player that looks like this: image
and here is the code:
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Column="4" Grid.Row="0" FontWeight="Bold">Pool</Label>
<Label Grid.Column="5" Grid.Row="0" FontWeight="Bold">Edge</Label>
<Label Grid.ColumnSpan="2" Grid.Row="0" HorizontalAlignment="Center" FontWeight="Bold">Felix</Label>
<Label Grid.Column="0" Grid.Row="1">Might</Label>
<Button x:Name="btMightP1" Margin="3" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="3, 0, 3, 0" Click="btMightP1_Click">+</Button>
<TextBox x:Name="tbMight1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="2" Grid.Column="2" Grid.Row="1" MinWidth="40">10</TextBox>
<Button x:Name="btMightM1" Margin="3" Grid.Column="3" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="4, 0, 4, 0" Click="btMightM1_Click">-</Button>
<TextBox Name="lbMightPool1" Grid.Row="1" Grid.Column="4" HorizontalAlignment="Center">0</TextBox>
<TextBox Name="lbMightEdge1" Grid.Row="1" Grid.Column="5" HorizontalAlignment="Center">0</TextBox>
<Label Grid.Column="0" Grid.Row="2">Speed</Label>
<Button x:Name="btSpeedP1" Margin="3" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="3, 0, 3, 0" Click="btSpeedP1_Click">+</Button>
<TextBox x:Name="tbSpeed1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="2" Grid.Column="2" Grid.Row="2" MinWidth="40">10</TextBox>
<Button x:Name="btSpeedM1" Margin="3" Grid.Column="3" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="4, 0, 4, 0" Click="btSpeedM1_Click">-</Button>
<TextBox Name="lbSpeedPool1" Grid.Row="2" Grid.Column="4" HorizontalAlignment="Center">0</TextBox>
<TextBox Name="lbSpeedEdge1" Grid.Row="2" Grid.Column="5" HorizontalAlignment="Center">0</TextBox>
<Label Grid.Column="0" Grid.Row="3">Intellect</Label>
<Button x:Name="btIntellectP1" Margin="3" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="3, 0, 3, 0" Click="btIntellectP1_Click">+</Button>
<TextBox x:Name="tbIntellect1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="2" Grid.Column="2" Grid.Row="3" MinWidth="40">10</TextBox>
<Button x:Name="btIntellectM1" Margin="3" Grid.Column="3" Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="4, 0, 4, 0" Click="btIntellectM1_Click">-</Button>
<TextBox Name="lbIntellectPool1" Grid.Row="3" Grid.Column="4" HorizontalAlignment="Center">0</TextBox>
<TextBox Name="lbIntellectEdge1" Grid.Row="3" Grid.Column="5" HorizontalAlignment="Center">0</TextBox>
</Grid>
code for Player class:
public class Player
{
public DataClass Might { get; set; }
public DataClass Speed { get; set; }
public DataClass Intellect { get; set; }
public string Name { get; set; }
public Player()
{
Might = new DataClass();
Speed = new DataClass();
Intellect = new DataClass();
}
and DataClass:
public class DataClass
{
public string Current { get; set; }
public string Pool { get; set; }
public string Edge { get; set; }
public DataClass()
{
Current = "0";
Pool = "0";
Edge = "0";
}
}
My question is how can I create identical grid every time I create an object of Player and tie the TextBoxes with newly created object's properties? Is this achievable only using data binding? I've tried to use it but I don't quite understand how the binding works.
Use listbox or ItemsControl.
<Listbox ItemSource="{Binding YourListOfPlayers}">
<Listbox.ItemTemplate>
<DataTemplate>
<Put your common grid here/>
</DataTemplate>
</Listbox.ItemTemplate>
</Listbox>
Related
this is my class TaskTodo and the method GetTask this method connect with sqlite and return a observable collection
using Microsoft.Data.Sqlite;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using Windows.Storage;
namespace uwp_to_do_list
{
public class TaskTodo : INotifyPropertyChanged
{
public string NameTask { get; set; }
public DateTime Reminder { get; set; }
public string NameList { get; set; }
public DateTime Date { get; set; }
public string SubTask { get; set; }
public string Priority { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ObservableCollection<TaskTodo> GetTasks()
{
var Tasks = new ObservableCollection<TaskTodo>();
//get data from sqlite3
string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "TasksSqlite.db");
using (SqliteConnection db =
new SqliteConnection($"Filename={dbpath}"))
{
db.Open();
SqliteCommand selectCommand = new SqliteCommand
("SELECT Name_task from Task", db);
SqliteDataReader query = selectCommand.ExecuteReader();
while (query.Read())
{
TaskTodo taskTodo = new TaskTodo();
taskTodo.NameTask = query.GetString(0);
Tasks.Add(taskTodo);
}
db.Close();
}
return Tasks;
}
so i put this observable collection as the itemsource of listview, but this just show the type of the object not the field NameTask.
public MainView()
{
this.InitializeComponent();
TaskTodo taskTodo = new TaskTodo();
task_list.ItemsSource = taskTodo.GetTasks()
Debug.WriteLine(taskTodo.GetTasks()[4].NameTask);
}
i think the problem is in the xaml code because went i tested if the observable collection is empty
Debug.WriteLine(taskTodo.GetTasks()[4].NameTask);
the outpuy was tasxk3
my xaml code
<Page
x:Class="uwp_to_do_list.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:uwp_to_do_list"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-
compatibility/2006" xmlns:fa="using:FontAwesome.UWP"
mc:Ignorable="d"
Background="#08090A"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Background="#04000E" Width="240"
Grid.Column="0"
Grid.Row="0" HorizontalAlignment="Left" >
<TextBlock Text="today" Foreground="White" FontSize="24"
HorizontalAlignment="Center" Margin="0,50,0,0">
</TextBlock>
<fa:FontAwesome Icon="CalendarCheckOutline" Height="40"
Width="40" FontSize="40" Foreground="Fuchsia"
HorizontalAlignment="Left" Margin="20,-35,0,0">
</fa:FontAwesome>
<TextBlock Text="Tomorrow" Foreground="White"
FontSize="24" HorizontalAlignment="Center"
Margin="0,20,0,0"></TextBlock>
<fa:FontAwesome Icon="CalendarOutline" Height="40"
Width="40" FontSize="40" Foreground="Fuchsia"
HorizontalAlignment="Left" Margin="20,-35,0,0">
</fa:FontAwesome>
<TextBlock Text="planning" Foreground="White"
FontSize="24" HorizontalAlignment="Center"
Margin="0,20,0,0"></TextBlock>
<fa:FontAwesome Icon="Calendar" Height="40" Width="40"
FontSize="40" Foreground="Fuchsia"
HorizontalAlignment="Left" Margin="20,-35,0,0">
</fa:FontAwesome>
<TextBlock Text="tasks" Foreground="White" FontSize="24"
HorizontalAlignment="Center" Margin="0,20,0,0">
</TextBlock>
<fa:FontAwesome Icon="Home" Height="40" Width="40"
FontSize="40" Foreground="Fuchsia"
HorizontalAlignment="Left" Margin="20,-35,0,0">
</fa:FontAwesome>
</StackPanel>
<TextBox Grid.Row="1" HorizontalAlignment="Left"
Grid.Column="0" Text="+ add a list " Background="#04000E"
Foreground="White" Height="50" Width="240" ></TextBox>
<TextBox x:Name="add_Task_textbox" Grid.Row="1"
Grid.Column="1" KeyDown="add_Task_textbox_KeyDown"
HorizontalAlignment="Center" VerticalAlignment="Center"
Text="+ add a task" Background="#04000E" Foreground="White"
Height="40" Width="1000" Margin="20,0,-20,0" />
<ListView x:Name="task_list" Grid.Row="0" Grid.Column="1"
Background="Transparent">
<ListViewItem>
<DataTemplate x:DataType="local:TaskTodo">
<TextBlock Width="100" Foreground="White" Text="{x:Bind
NameTask}"></TextBlock>
</DataTemplate>
</ListViewItem>
</ListView>
</Grid>
</Page>
for more context my github repository
You should set the ItemTemplate property to your DataTemplate instead of adding a ListViewItem:
<ListView x:Name="task_list" Grid.Row="0" Grid.Column="1"
Background="Transparent">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:TaskTodo">
<TextBlock Width="100" Foreground="White"
Text="{x:Bind NameTask}" />
</DataTemplate>
<ListView.ItemTemplate>
</ListView>
I am trying to make some music library app for my classes. I've done pretty much everything i had to but there is one thing left. I don't know how to access data from data template generated item to delete both this item form app and xml node from xml file.
I have found this bit of code that should delete the node i want but i still don't know how to access data from item that i selected.
private void DeleteFromFile_Click(object sender, RoutedEventArgs e)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(#"musicLibrary.xml");
foreach(XmlNode xNode in xDoc.SelectNodes("musicLibrary"))
{
if(xNode.SelectSingleNode("title").InnerText == "")
{
xNode.ParentNode.RemoveChild(xNode);
}
}
}
the nodes looks like this:
<musicLibrary>
<title>I Still Haven't Found What I'm Looking For</title>
<albumName>The Joshua Tree</albumName>
<artist>U2</artist>
<releaseDate>1987</releaseDate>
<tempo>101</tempo>
<duration>4:37</duration>
<genre>Rock</genre>
</musicLibrary>
<musicLibrary>
<title>Magnificent</title>
<albumName>No Line on the Horizon</albumName>
<artist>U2</artist>
<releaseDate>2009</releaseDate>
<tempo>115</tempo>
<duration>5:24</duration>
<genre>Rock</genre>
</musicLibrary>
data templates that i use:
<Page.Resources>
<CollectionViewSource x:Key="listDataView"/>
<DataTemplate x:Key="mDataTamplate">
<StackPanel>
<TextBlock FontSize="12" Margin="5" Text="{Binding title}" />
<TextBlock FontSize="10" Margin="5" Text="{Binding artist}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="detailedDataTamplate">
<Grid Grid.Row="3" Margin="20,0,20,0">
<!-- Row/Column Definition-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.7*"/>
<ColumnDefinition Width="1.5*"/>
<ColumnDefinition Width="0.7*"/>
<ColumnDefinition Width="0.7*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- First Column -->
<TextBlock Grid.Column="0" Grid.Row="0" Text="Title" Style="{StaticResource textBlockStyle}" />
<TextBlock Text="{Binding title}"
Grid.Column="1" Grid.Row="0"
Style="{StaticResource textBlockStyle}"/>
<TextBlock Grid.Column="0" Grid.Row="1" Text="Album name" Style="{StaticResource textBlockStyle}"/>
<TextBlock Text="{Binding albumName}"
Grid.Column="1" Grid.Row="1"
Style="{StaticResource textBlockStyle}"/>
<TextBlock Grid.Column="0" Grid.Row="2" Text="Artist" Style="{StaticResource textBlockStyle}"/>
<TextBlock Text="{Binding artist}"
Grid.Column="1" Grid.Row="2"
Style="{StaticResource textBlockStyle}"/>
<TextBlock Grid.Column="0" Grid.Row="3" Text="Release Date" Style="{StaticResource textBlockStyle}"/>
<TextBlock Text="{Binding releaseDate}"
Grid.Column="1" Grid.Row="3"
Style="{StaticResource textBlockStyle}"/>
<!-- Second Column -->
<TextBlock Grid.Column="2" Grid.Row="0" Text="Tempo" Style="{StaticResource textBlockStyle}" />
<TextBlock Text="{Binding tempo}"
Grid.Column="3" Grid.Row="0"
Style="{StaticResource textBlockStyle}"/>
<TextBlock Grid.Column="2" Grid.Row="1" Text="Duration" Style="{StaticResource textBlockStyle}"/>
<TextBlock Grid.Column="3" Grid.Row="1"
Text="{Binding duration}"
Style="{StaticResource textBlockStyle}"/>
<TextBlock Grid.Column="2" Grid.Row="2" Text="Genre" Style="{StaticResource textBlockStyle}"/>
<TextBlock Grid.Column="3" Grid.Row="2"
Text="{Binding genre}"
Style="{StaticResource textBlockStyle}"/>
<Button x:Name="saveToFile" Click="saveToFile_Click" Content="Save to file" Grid.Column="2" Grid.Row="3" Grid.ColumnSpan="1" VerticalAlignment="Center" Height="31"/>
<Button x:Name="deleteFromFile" Click="DeleteFromFile_Click" Content="Delete" Grid.Column="3" Grid.Row="3"/>
</Grid>
</DataTemplate>
</Page.Resources>
And class that i bind data from:
namespace MLUI
{
[XmlRoot("dataSet")]
public class DataSet
{
[XmlElement("musicLibrary")]
public List<Song> Songs { get; set; }
}
public class Song
{
[XmlElement("title")]
public string title { get; set; }
[XmlElement("artist")]
public string artist { get; set; }
[XmlElement("albumName")]
public string albumName { get; set; }
[XmlElement("releaseDate")]
public string releaseDate { get; set; }
[XmlElement("tempo")]
public int tempo { get; set; }
[XmlElement("duration")]
public string duration { get; set; }
[XmlElement("genre")]
public string genre { get; set; }
public override string ToString()
{
return "Title: " + title +"\r\nArtist: "+ artist +"\r\nAlbum Name: "+ albumName + "\r\nRelease Date: " + releaseDate +
"\r\nTempo "+ tempo + "\r\nDuration " + duration + "\r\nGenre " + genre ;
}
}
}
I like using Xml Linq with a dictionary :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement dataSet = doc.Root;
Dictionary<string, XElement> dictionary = dataSet.Elements("musicLibrary")
.GroupBy(x => (string)x.Element("title"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
XElement Magnificent = dictionary["Magnificent"];
Magnificent.Remove();
}
}
}
I have WPF project "LittleLibrary" and and it must enable adding and deleting books. So in Xaml i have form and one button
Here is how it looks like https://imgur.com/1N7PYWm
and in C# code i'm trying to serialization like this (I'm using Newtonsoft.Json)
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string Genre { get; set; }
public int Year { get; set; }
public string Cover { get; set; }
public int Availability { get; set; }
public string Description { get; set; }
}
public class RootObject
{
public List<Book> Book { get; set; }
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
RootObject rootObjectSerializacja = new RootObject();
Book newBook = new Book();
newBook.Title = titleTextBox.Text;
newBook.Author = authorTextBox.Text;
newBook.Genre = gatunekTextBox.Text;
newBook.Year = int.Parse(rokWydaniaTextBox.Text);
newBook.Cover = okladkaTextBox.Text;
newBook.Availability = int.Parse(dostepnoscTextBox.Text);
newBook.Description = opisTextBox.Text;
rootObjectSerializacja.Book.Add(newBook);
string content = JsonConvert.SerializeObject(rootObjectSerializacja);
File.WriteAllText("newJsonFile.json", content);
}
Code from my Xaml :
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="200"></ColumnDefinition>
<ColumnDefinition Width="*" MinWidth="300"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="7*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<!--Lewa strona -->
<Label x:Name="titleLabel" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" VerticalAlignment="Center">Title:</Label>
<TextBox x:Name="titleTextBox" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150"></TextBox>
<Label x:Name="authorLabel" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Center">Author:</Label>
<TextBox x:Name="authorTextBox" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150"></TextBox>
<Label x:Name="gatunekLabel" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Center">Gentre:</Label>
<TextBox x:Name="gatunekTextBox" Grid.Column="1" Grid.Row="2" Width="150" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBox>
<Label x:Name="rokLabel" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Right" VerticalAlignment="Center">Year:</Label>
<TextBox x:Name="rokWydaniaTextBox" Grid.Column="1" Grid.Row="3" Width="150" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBox>
<Label x:Name="okladkaLabel" Grid.Column="0" Grid.Row="4" HorizontalAlignment="Right" VerticalAlignment="Center">Cover:</Label>
<TextBox x:Name="okladkaTextBox" Grid.Column="1" Grid.Row="4" Width="150" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBox>
<Label x:Name="dostepnoscLabel" Grid.Column="0" Grid.Row="5" HorizontalAlignment="Right" VerticalAlignment="Center">Avb:</Label>
<TextBox x:Name="dostepnoscTextBox" Grid.Column="1" Grid.Row="5" Width="150" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBox>
<Label x:Name="opisLabel" Grid.Column="0" Grid.Row="6" HorizontalAlignment="Right" VerticalAlignment="Center">Description:</Label>
<TextBox x:Name="opisTextBox" Grid.Column="1" Grid.Row="6" Width="150" ></TextBox>
</Grid>
<Button x:Name="btnAdd" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150" Margin="10" Click="btnAdd_Click" >ADD</Button>
</Grid>
</Grid>
The problem is i have an
Exception {"The object reference has not been set to the instance of the object."} System.NullReferenceException
And content is NULL. What i'm doing wrong ?
Try adding a constructor in your RootObject class.
public class RootObject
{
public List<Book> Book { get; set; }
public RootObject()
{
Book = new List<string>();
}
}
I'm developing a WPF App using MVVM architecture but I'm having an issue setting a current Model to a ViewModel.
Well, I'm currently trying to set a Model coming from a ViewModel (A:SectionCustomersViewModel) to another one (B:CustomerViewModel) but everytime the "currentModel" (CustomerViewModel's property) is setted it's just null and obviously the binded View to the CustomerViewModel shows nothing in controls.
I have already tried setting the model directly to the ViewModel but that didn't work, so I changed the strategic using a kind of publisher-subscriber pattern but it didn't work either.
It should be mentioned that i have done test with some data dummy to see if the user controls (textbox, combobox, etc.) are setted and effectively they are.
Time to see the code for more clearness (for a clearer and shorter code I just remove no-needed lines).
ViewModel A (SectionCustomersViewModel).
class SectionCustomersViewModel : ObservableObject, IPageViewModel
{
private event Action<CustomerModel> CustomerToCustomerViewModel;
public string Name => "Customers".ToUpper();
public string Icon => "images/UserGroups_96px.png";
private IPageViewModel _currentPageViewModel;
private ICommand _changePageCommand;
private ICommand _addCustomerCommand;
private ICommand _editCustomerCommand;
public SectionCustomersViewModel()
{
// Add available pages
CustomersViewModel = new CustomersViewModel();
CustomerViewModel = new CustomerViewModel();
CustomerToCustomerViewModel += (CustomerViewModel as CustomerViewModel).ReceiveCustomer;
// Set starting page
CurrentPageViewModel = CustomersViewModel;
}
#region Properties
#region Pages
public IPageViewModel CustomersViewModel { get; }
public IPageViewModel CustomerViewModel { get; }
#endregion
public IPageViewModel CurrentPageViewModel
{
get => _currentPageViewModel;
set
{
if (_currentPageViewModel != value)
{
_currentPageViewModel = value;
OnPropertyChanged("CurrentPageViewModel");
}
}
}
#endregion
#region Commands
public ICommand BackPageCommand
{
get
{
if (_changePageCommand == null)
_changePageCommand = new CommandHandler(p => ChangeViewModel(p as IPageViewModel), p => p is IPageViewModel);
return _changePageCommand;
}
}
public ICommand AddCustomerCommand
{
get
{
if (_addCustomerCommand == null)
_addCustomerCommand = new CommandHandler(param => AddCustomer());
return _addCustomerCommand;
}
}
public ICommand EditCustomerCommand
{
get
{
if (_editCustomerCommand == null)
_editCustomerCommand = new CommandHandler(c => EditCustomer(c as CustomerModel));
return _editCustomerCommand;
}
}
#endregion
#region Methods
private void ChangeViewModel(IPageViewModel viewModel)
=> CurrentPageViewModel = viewModel;
private void EditCustomer(CustomerModel customer)
{
CurrentPageViewModel = CustomerViewModel;
CustomerToCustomerViewModel?.Invoke(customer);
}
private void AddCustomer()
{
CurrentPageViewModel = CustomerViewModel;
CustomerToCustomerViewModel?.Invoke(new CustomerModel());
}
#endregion
}
View for the ViewModel A
<UserControl x:Class="Presentation.View.Customer.SectionCustomersView"
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:vm="clr-namespace:Presentation.ViewModel.Customer"
xmlns:v="clr-namespace:Presentation.View.Customer"
mc:Ignorable="d" d:DesignWidth="800" Height="Auto">
<UserControl.Resources>
<vm:SectionCustomersViewModel x:Key="SectionCustomersVM"/>
<DataTemplate DataType="{x:Type vm:CustomersViewModel}">
<v:CustomersView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:CustomerViewModel}">
<v:CustomerView />
</DataTemplate>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Source="{StaticResource SectionCustomersVM}"/>
</UserControl.DataContext>
<Grid>
<ContentControl Content="{Binding CurrentPageViewModel}" />
</Grid>
ViewModel B (CustomerViewModel).
class CustomerViewModel : ObservableObject, IPageViewModel
{
#region Attributes
private CustomerModel _currentCustomer;
private AddressModel _currentAddress;
private ICommand _loadedCustomerCommand;
private ICommand _saveCustomerCommand;
#endregion
#region IPageViewModel's properties
public string Name => "Customer";
public string Icon => string.Empty;
#endregion
#region Properties
public Action<CustomerModel> ReceiveCustomer
=> SetCustomer;
public List<CountryModel> Countries => CountryModel.Load();
public List<ZoneModel> Zones => ZoneModel.Load();
public List<CustomerGroupModel> CustomerGroups => CustomerGroupModel.Load();
public CustomerModel CurrentCustomer
{
get => _currentCustomer;
set
{
if (value != _currentCustomer)
{
_currentCustomer = value;
OnPropertyChanged("CurrentCustomer");
}
}
}
public AddressModel CurrentAddress
{
get => _currentAddress;
set
{
if (value != _currentAddress)
{
_currentAddress = value;
OnPropertyChanged("CurrentAddress");
}
}
}
#endregion
#region Commands
public ICommand LoadedCommand
{
get
{
if (_loadedCustomerCommand == null)
_loadedCustomerCommand = new CommandHandler(o => LoadExtraData());
return _loadedCustomerCommand;
}
}
public ICommand SaveCommand
{
get
{
if (_saveCustomerCommand == null)
{
_saveCustomerCommand = new CommandHandler(
param => SaveCustomer(),
param => (CurrentCustomer != null)
);
}
return _saveCustomerCommand;
}
}
#endregion
#region Methods
private void SetCustomer(CustomerModel customer)
=> CurrentCustomer = customer;
private void LoadExtraData()
{
//if (CurrentCustomer.Address_id == 0)
// return;
//// Dummy data, just to test if the binding with the view is correctly setted.
//CurrentAddress = new AddressModel()
//{
// Firstname = "John",
// Lastname = "Doe",
// Address_1 = "Nowhere",
// City = "Somewhere",
// Company = "A-Z",
// Country_id = 1
//};
//// If the currentCustomer is setted here, it works fine.
//CurrentCustomer = new CustomerModel()
//{
// Firstname = "Some",
// Lastname = "One",
// Email = "somemail#mail.com",
// Telephone = "555-000-000",
// Status = true,
// Safe = false,
// Newsletter = false,
// Customer_group_id = 1
//};
}
private void SaveCustomer()
{
// You would implement your Product save here
CurrentAddress.Save();
CurrentCustomer.Save();
}
#endregion
}
View for the ViewModel B
<UserControl x:Class="Presentation.View.Customer.CustomerView"
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:vm="clr-namespace:Presentation.ViewModel.Customer"
xmlns:v="clr-namespace:Presentation.View.Customer"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
VerticalAlignment="Stretch"
VerticalContentAlignment="Stretch">
<UserControl.Resources>
<vm:CustomerViewModel x:Key="CustomerViewModel" />
</UserControl.Resources>
<UserControl.DataContext>
<Binding Source="{StaticResource CustomerViewModel}" />
</UserControl.DataContext>
<!-- Bind UserControl Loaded event to CustomersVM's LoadedCommand -->
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="160*" />
<RowDefinition Height="25*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="32" Height="32"
Command="{Binding DataContext.BackPageCommand, RelativeSource={RelativeSource AncestorType={x:Type v:SectionCustomersView}}}"
CommandParameter="{Binding DataContext.CustomersViewModel, RelativeSource={RelativeSource AncestorType={x:Type v:SectionCustomersView}}}">
<Image Source="/Presentation;Component/images/BackArrow_32px.png" Stretch="Uniform"/>
</Button>
<Label Grid.Row="0" Content="{Binding Name}" HorizontalAlignment="Left" VerticalAlignment="Top"
HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Width="Auto" FontSize="24" FontFamily="Century Gothic" FontStretch="Expanded" BorderBrush="#FF4C4D5A" Height="37" Margin="67,10,0,0"/>
<Label Grid.Row="0" Content="{Binding Icon}" HorizontalAlignment="Left" VerticalAlignment="Top"
HorizontalContentAlignment="Left" VerticalContentAlignment="Bottom" Width="Auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Margin="154,10,0,0" Foreground="#FF464646"/>
<Separator Grid.Row="0" Height="20" Margin="10,52,10,0" VerticalAlignment="Top"/>
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto"
HorizontalContentAlignment="Center" VerticalContentAlignment="Top">
<WrapPanel>
<GroupBox Header="Detalles" FontFamily="Century gothic" Height="Auto" Width="367"
VerticalContentAlignment="Top" VerticalAlignment="Top">
<Grid Margin="0">
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Width="Auto" Margin="35,10,0,0">
<Label Grid.Row="1" Content="Firstname"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<TextBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" TextWrapping="NoWrap" Width="135"
Text="{Binding CurrentCustomer.Firstname}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,52,0,0">
<Label Grid.Row="1" Content="Lastname"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<TextBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" TextWrapping="NoWrap" Width="135"
Text="{Binding CurrentCustomer.Lastname}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,94,0,0">
<Label Grid.Row="1" Content="Email"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<TextBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" TextWrapping="NoWrap" Width="135"
Text="{Binding CurrentCustomer.Email}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,136,0,0">
<Label Grid.Row="1" Content="Telephone"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<TextBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" TextWrapping="NoWrap" Width="135"
Text="{Binding CurrentCustomer.Telephone}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,178,0,0">
<Label Grid.Row="1" Content="Customer Group"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<ComboBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" Width="135"
ItemsSource="{Binding CustomerGroups}"
SelectedValuePath="Customer_Group_ID" DisplayMemberPath="Name"
SelectedValue="{Binding CurrentCustomer.Customer_group_id}"/>
</StackPanel>
</Grid>
</GroupBox>
<GroupBox Header="Domicilio" FontFamily="Century gothic" Height="377" Width="376"
VerticalAlignment="Top" VerticalContentAlignment="Top">
<Grid Margin="0">
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Width="Auto" Margin="35,10,0,0">
<Label Grid.Row="1" Content="Name"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<TextBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" TextWrapping="NoWrap" Width="135"
Text="{Binding CurrentAddress.Firstname}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,52,0,0">
<Label Grid.Row="1" Content="Lastname"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<TextBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" TextWrapping="NoWrap" Width="135"
Text="{Binding CurrentAddress.Lastname}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,94,0,0">
<Label Grid.Row="1" Content="Company"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<TextBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" TextWrapping="NoWrap" Width="135"
Text="{Binding CurrentAddress.Company}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,136,0,0">
<Label Grid.Row="1" Content="Address"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<TextBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" TextWrapping="NoWrap" Width="135"
Text="{Binding CurrentAddress.Address_1}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,178,10,0">
<Label Content="Ciudad"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<TextBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" TextWrapping="NoWrap" Width="135"
Text="{Binding CurrentAddress.City}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,220,10,0">
<Label Grid.Row="1" Content="Postcode"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<TextBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" TextWrapping="NoWrap" Width="135"
Text="{Binding CurrentAddress.Postcode}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,262,0,0">
<Label Grid.Row="1" Content="Country"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<ComboBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" Width="135"
ItemsSource="{Binding Countries}"
DisplayMemberPath="Name" SelectedValuePath="Country_ID"
SelectedValue="{Binding CurrentAddress.Country_id}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="Auto"
VerticalAlignment="Top" Margin="35,304,10,-13">
<Label Grid.Row="1" Content="Region"
HorizontalAlignment="Left" VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" Width="auto" FontSize="16" FontFamily="Century Gothic" FontStretch="Expanded" Height="37" Foreground="#FF464646"/>
<ComboBox HorizontalAlignment="Right" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
Height="23" Width="135" ItemsSource="{Binding Zones}"
DisplayMemberPath="Name" SelectedValuePath="Zone_ID"
SelectedValue="{Binding CurrentAddress.Zone_id}"/>
</StackPanel>
</Grid>
</GroupBox>
<GroupBox Header="Otro" FontFamily="Century gothic" Height="auto" Width="Auto"
VerticalAlignment="Top" VerticalContentAlignment="Top">
<Grid Margin="0">
<StackPanel Orientation="Vertical"
VerticalAlignment="Top" Margin="10">
<CheckBox HorizontalAlignment="Center" Style="{DynamicResource CheckBoxSlider}"
VerticalAlignment="Center" Content="Newsletter" FontSize="16" FontStretch="Expanded"
FontFamily="Century Gothic" Foreground="#FF464646" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Width="255" Margin="30,8"
IsChecked="{Binding CurrentCustomer.Newsletter}"/>
<CheckBox HorizontalAlignment="Center" Style="{DynamicResource CheckBoxSlider}"
VerticalAlignment="Center" Content="Status" FontSize="16" FontStretch="Expanded"
FontFamily="Century Gothic" Foreground="#FF464646" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Width="255" Margin="10,8"
IsChecked="{Binding CurrentCustomer.Status}"/>
<CheckBox HorizontalAlignment="Center" Style="{DynamicResource CheckBoxSlider}"
VerticalAlignment="Center" Content="Safe" FontSize="16" FontStretch="Expanded"
FontFamily="Century Gothic" Foreground="#FF464646" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Width="255" Margin="10,8"
IsChecked="{Binding CurrentCustomer.Safe}"/>
</StackPanel>
</Grid>
</GroupBox>
</WrapPanel>
</ScrollViewer>
<Button Content="Accept" Grid.Row="2" Height="30"
VerticalAlignment="Bottom" HorizontalAlignment="Right"
Width="80" Margin="0,0,10,10"
Command="{Binding SaveCommand}"/>
</Grid>
<Page.Resources>
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" x:Name="testTest" />
</ResourceDictionary>
</Page.Resources>
itemscontrol:
<Grid Grid.Row="1" Grid.ColumnSpan="2" Name="testName">
<ScrollViewer VerticalScrollBarVisibility="Hidden" PanningMode="Both">
<ItemsControl ItemsSource="{Binding Path=NextMeetingList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Grid.Column="0" Grid.Row="0">
<Border x:Name = "myVariable" Grid.Column="0" Grid.Row="0" Margin="10" Height="30" Background="#A2C2E7" CornerRadius="5" BorderBrush="#A2C2E7">
<Grid Margin="8,0,8,0" Background="#A2C2E7">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Foreground="White" FontWeight="Bold" FontSize="15" Margin="0,4,0,0" HorizontalAlignment="Left">month</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Foreground="White" FontWeight="Bold" FontSize="15" Margin="0,4,0,0" HorizontalAlignment="Right">1 Meeting</TextBlock>
</Grid>
</Border>
<Border Grid.Column="0" Grid.Row="0" Margin="10" Height="60" Background="GhostWhite" CornerRadius="3" BorderBrush="{Binding BorderColor}" BorderThickness="0,8,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="118"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="40" Margin="10,5,0,0" Style="{DynamicResource Lato-Semibold}" Text="{Binding endDate.Day}"/>
<TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="12" Margin="77,13,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text="{Binding DayString}"/>
<TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="14" Margin="70,26,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text="{Binding endDate.Hour}"/>
<TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="14" Margin="86,26,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text=":"/>
<TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="14" Margin="90,26,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text="{Binding MinuteString}"/>
<Border Grid.Row="0" Grid.Column="0" Width="1" BorderBrush="#BABABA" Height="40" Margin="115,-6,0,0" BorderThickness="1" HorizontalAlignment="Left"></Border>
<TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Text="{Binding subject}" FontWeight="Bold" FontSize="17" Margin="10,10,0,0"/>
<TextBlock Grid.Row="0" Grid.Column="1" Width="Auto" TextWrapping="Wrap" HorizontalAlignment="Left" Text="{Binding descr}" FontSize="10" Margin="20,27,150,0"/>
<TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Text="{Binding companyName}" FontSize="10" Margin="0,10,30,0"/>
<TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Text="sala del consiglio" FontSize="10" Margin="0,27,30,0"/>
<TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Text=">" FontSize="15" VerticalAlignment="Center" Margin="0,-5,10,0"/>
</Grid>
</Border>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
Hello everyone , I should be able to make a grid Collapsed inside a ItemsControl in WPF .
The problem is that I can not understand how to use the booleanToVisibilityConverter from code behind .
Maybe I'm missing out on a glass of water but I can not connect how to do, if the name TestTest septum can not see it then the code-behind .
from code behind i don't see "myVariable"
To get hold of the converter from code-behind, you'd have to look it up in the Page's Resources dictionary. Do something like this in your Page class:
IValueConverter converter = this.Resources["booleanToVisibilityConverter"] as IValueConverter;
Giving the converter's resource entry a Name is not necessary; here you want to use the Key. Also, converters are normally used with XAML bindings -- if you want one in code, you can just instantiate one:
IValueConverter converter = new BooleanToVisibilityConverter();
Common usage goes something like this:
<Window.Resources>
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="Converter" />
</ResourceDictionary>
</Window.Resources>
<Grid Visibility="{Binding Display, Converter={StaticResource Converter}}">
...
</Grid>
Code-behind, using the window class itself as the data context:
public partial class MainWindow
{
public bool Display { get; set; };
public MainWindow()
{
InitializeComponent();
DataContext = this; // For the binding
Display = true;
}
}
Okay, one more, this time without data binding or converter:
<Grid x:Name="myGrid">
...
</Grid>
Code-behind:
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
myGrid.Visibility = Visibility.Visible;
}
}
The above doesn't explain why the OP couldn't reference "myVariable" from code-behind. That's because it's in a template, which is instantiated (dynamically) once per item in the collection.
It's still possible to get at it from code -- dynamically. For example, this XAML uses a DataTemplate:
<ItemsControl ItemsSource="{Binding Names}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid x:Name="innerGrid" Loaded="OnGridLoaded" DataContext="{Binding Name, Mode=OneWay}">
<TextBox Text="{Binding Path=., Mode=OneWay}" />
</Grid>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The following code-behind lets us reach into the template and make "innerGrid" disappear selectively:
public class Data
{
public string Name { get; }
public Data(string name)
{
Name = name;
}
}
public partial class MainWindow
{
public Data[] Names { get; } = { new Data("Hello"), new Data("World") };
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void OnGridLoaded(object sender, RoutedEventArgs e)
{
Grid outerGrid = sender as Grid;
Grid innerGrid = outerGrid?.FindName("innerGrid") as Grid;
if (innerGrid != null)
{
string name = innerGrid.DataContext as string;
if (name == "Hello")
{
innerGrid.Visibility = Visibility.Collapsed;
}
}
}
}
(To make the string easily accessible I bound the Data object to the "innerGrid" DataContext.)