Styling a WPF Combobox with a Textbox-Item - wpf

I have a Combobox. Each item consists of a headline and a description.
I want to use in the second item a text box. This works well. ;)
Now I wanted to ask (because the item with the text box is higher than all the others), whether it is possible that if the item which is selected (with the textbox) does not displayed the textbox, only the contents as a string?
<ComboBox Height="35" Margin="0 2 0 2" SelectedIndex="0">
<ComboBoxItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="/WpfApplication14;component/Resources/Icon1.ico" Height="22" Width="22" Grid.Column="0" HorizontalAlignment="Left" />
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
<RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
</Grid.RowDefinitions>
<TextBlock Text="Item Titel 1" Grid.Row="0" FontWeight="Bold" />
<TextBlock Text="Item Beschreibung 1" Grid.Row="1" FontStyle="Italic">
<TextBlock.TextEffects><TextEffect Foreground="#FF555454" /></TextBlock.TextEffects>
</TextBlock>
</Grid>
</Grid>
</ComboBoxItem>
<ComboBoxItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="/WpfApplication14;component/Resources/Icon2.ico" Height="22" Width="22" Grid.Column="0" HorizontalAlignment="Left" />
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
<RowDefinition Height="24" MinHeight="24" MaxHeight="24" />
</Grid.RowDefinitions>
<TextBlock Text="Item Titel 2" Grid.Row="0" FontWeight="Bold" />
<TextBox Grid.Row="1">
<TextBox.Text>c:\temp\test</TextBox.Text>
<TextBox.Style>
<Style>
<Setter Property="TextBox.Height" Value="20"/>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</Grid>
</ComboBoxItem>
</ComboBox>

You can achieve this by using a TextBox to be able to edit the text (like you've done) and a TextBlock to display the text when the item is selected.
You can show/hide the TextBlock/TextBox by binding the Visibility of them to the ComboBoxItem's IsSelected value and use a ValueConverter to convert the true/false value to Visible/Collapsed.
I've edited your code a bit here to make it simpler as well:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ComboBox Name="myComboBox" Margin="0 2 0 2" SelectedIndex="0" Grid.Row="1">
<ComboBoxItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
<RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
</Grid.RowDefinitions>
<TextBlock Text="Item Titel 1" Grid.Row="0" FontWeight="Bold" />
<TextBlock Text="Item Beschreibung 1" Grid.Row="1" FontStyle="Italic" Foreground="#FF555454" />
</Grid>
</Grid>
</ComboBoxItem>
<ComboBoxItem Name="myComboBoxItem">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Item Titel 2" Grid.Row="0" FontWeight="Bold" />
<TextBox Grid.Row="1" Name="myTextBox" Text="c:\temp\test" Height="20" Visibility="{Binding ElementName=myComboBoxItem, Path=IsSelected, Converter={ValueConverter:BooleanToVisibilityConverter}}" />
<TextBlock Name="myTextBlock" Text="{Binding ElementName=myTextBox, Path=Text}" Grid.Row="1" FontStyle="Italic" Foreground="#FF555454" Visibility="{Binding ElementName=myComboBoxItem, Path=IsSelected, Converter={ValueConverter:BooleanToVisibilityConverter}, ConverterParameter=Inverted}" />
</Grid>
</Grid>
</ComboBoxItem>
</ComboBox>
</Grid>
The code for the ValueConverter:
public abstract class BaseConverter : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
public class BooleanToVisibilityConverter : BaseConverter, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter != null && parameter.ToString().Equals("Inverted"))
{
if ((bool)value)
return Visibility.Visible;
return Visibility.Collapsed;
}
if ((bool)value)
return Visibility.Collapsed;
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
if (parameter.ToString().Equals("Inverted"))
return (Visibility)value != Visibility.Visible;
return (Visibility)value == Visibility.Visible;
}
catch (Exception e)
{
// Error handling
return false;
}
}
}

replace:
<TextBox Grid.Row="1">
<TextBox.Text>c:\temp\test</TextBox.Text>
<TextBox.Style>
<Style>
<Setter Property="TextBox.Height" Value="20"/>
</Style>
</TextBox.Style>
</TextBox>
with:
<TextBox Grid.Row="1" Text="c:\temp\test" Height="20" BorderThickness="0" Padding="0" />
but you will still have an issue:
your comboBox is 35 pixels high:
<ComboBox Height="35" Margin="0 2 0 2" SelectedIndex="0">
but your ComboBoxItem is 16 + 24 = 40 pixels high:
<Grid.RowDefinitions>
<RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
<RowDefinition Height="24" MinHeight="24" MaxHeight="24" />
</Grid.RowDefinitions>
so it will be truncated anyway. Either make your comboBox bigger or your comboBoxItem smaller.
side Note: you don't need to use a text effect for the foreground on the textblock:
<TextBlock Foreground="#FF555454" />
would work just as well

Related

WPF MVVM Combobox SelectedItem not working properly

I've already searched and read from several posts but I couldn't find what I am doing wrong.
I have a ComboBox with an ObservableCollection<string> Available_COMPorts as ItemsSource.
On SelectedValue I binded a string named SelectedCOMPort with Mode = TwoWay.
<Label Content="Porta COM: "></Label>
<ComboBox ItemsSource="{Binding Available_COMPorts}"
SelectedValue="{Binding SelectedCOMPort, Mode=TwoWay}" />
After bofere the combobox, I have a Label displaying the SelectedCOMPort
<Label Content="Status: " />
<Label Content="{Binding SelectedCOMPort}" Foreground="Red" />
I have made both Available_COMPorts and SelectedCOMPort with INotifyPropertyChanged. On my ViewModel Initialization, I filled the Available_COMPorts with the three SerialPort strings available ("COM6", "COM5", "COM4") and set SelectedCOMPort = "Available_COMPorts[0]" ("COM6").
When I Run the code, ComboBox has the three itens, the selected item is "COM6" and the label shows "COM6" (everything is fine). Then I select "COM5" and the label updates it's value to "COM5". This is presented on the following pictures.
The problem is when I try to access SelectedCOMPort on the ViewModel, as I need the selected item to connect on my SerialPort. The SelectedCOMPort is always as the default value "COM6". I try to access the SelectedCOMPorton the connect click command.
Debbuging, using Breakpoints on INotifyProperty functions I realized that the binding property seems to be working fine, but when it leaves INotifyProperty the value goes back to default.
Why is that happening? I've tryed several approachs and none worked for me.
Following is my ViewModel and BaseViewModel with INotifyProperty:
BaseViewModel : INotifyPropertyChanged
public class BaseViewModel : INotifyPropertyChanged
{
#region PropertyChange
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
protected void SetProperty<T>(ref T backingField, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(backingField, value)) return;
backingField = value;
OnPropertyChanged(propertyName);
}
#endregion
}
ViewModel
public class LiveGraphViewModel : BaseViewModel
{
public ConnectButtonCommand ConnectButtonCommand { get; set; }
private ObservableCollection<string> _availableCOMPorts;
private string _selectedCOMPort;
public ObservableCollection<string> Available_COMPorts
{
get { return _availableCOMPorts; }
set { SetProperty(ref _availableCOMPorts, value); }
}
public string SelectedCOMPort
{
get { return _selectedCOMPort; }
set { SetProperty(ref _selectedCOMPort, value); }
}
public LiveGraphViewModel()
{
this.ConnectButtonCommand = new ConnectButtonCommand(this);
ObservableCollection<string> TempCOM = new ObservableCollection<string>();
foreach (string comport in SerialPort.GetPortNames())
TempCOM.Add(comport);
Available_COMPorts = TempCOM;
if(Available_COMPorts.Count > 0)
SelectedCOMPort = Available_COMPorts[0];
}
public void ConnectButton()
{
if (SelectedCOMPort == "COM5")
Connect(SelectedCOMPort);
}
}
LiveGraphView XAML
<UserControl x:Class="SmartAthleticsWPF.Views.LiveGraphView"
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:SmartAthleticsWPF.Views"
xmlns:viewmodels="clr-namespace:SmartAthleticsWPF.ViewModels"
xmlns:commands="clr-namespace:SmartAthleticsWPF.Commands"
xmlns:syncfusion="clr-namespace:Syncfusion.UI.Xaml.Charts;assembly=Syncfusion.SfChart.WPF"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<viewmodels:LiveGraphViewModel x:Key="LIVviewModel"/>
</UserControl.Resources>
<Grid Background="#EDEDED">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.01*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.01*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="1.2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="0.1*"/>
</Grid.RowDefinitions>
<!--LIVE GRAPH BORDER-->
<Border Grid.Row="1" Grid.RowSpan="2"
Grid.Column="1" Grid.ColumnSpan="2"
Margin="5"
BorderBrush="Black"
BorderThickness="2"
CornerRadius="5"
Padding="10,10,30,10"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<DockPanel>
<Label Content="Live Graph"
DockPanel.Dock="Top"
HorizontalAlignment="Center"
Padding="0,5,0,10" FontSize="22"/>
<syncfusion:SfChart x:Name="LiveGraphChart" >
<syncfusion:SfChart.PrimaryAxis>
<syncfusion:NumericalAxis Header="Seconds"
Maximum="{Binding MaxXAxis}"
Minimum="{Binding MinXAxis}"/>
</syncfusion:SfChart.PrimaryAxis>
<syncfusion:SfChart.SecondaryAxis>
<syncfusion:NumericalAxis Header="Kgf"/>
</syncfusion:SfChart.SecondaryAxis>
<syncfusion:SfChart.Series>
<syncfusion:FastLineBitmapSeries ItemsSource="{Binding FyCircularBuffer}"
XBindingPath="XData" YBindingPath="YData"
StrokeThickness="1" Interior="DarkGreen"
ShowTooltip="False" ShowTrackballInfo="False"
IsSeriesVisible="{Binding FyChecked}"/>
<syncfusion:FastLineBitmapSeries ItemsSource="{Binding MyCircularBuffer}"
XBindingPath="XData" YBindingPath="YData"
StrokeThickness="1" Interior="LimeGreen"
ShowTooltip="False" ShowTrackballInfo="False"
IsSeriesVisible="{Binding MyChecked}"/>
<syncfusion:FastLineBitmapSeries ItemsSource="{Binding FxCircularBuffer}"
XBindingPath="XData" YBindingPath="YData"
StrokeThickness="1" Interior="IndianRed"
ShowTooltip="False" ShowTrackballInfo="False"
IsSeriesVisible="{Binding FxChecked}"/>
<syncfusion:FastLineBitmapSeries ItemsSource="{Binding MxCircularBuffer}"
XBindingPath="XData" YBindingPath="YData"
StrokeThickness="1" Interior="Red"
ShowTooltip="False" ShowTrackballInfo="False"
IsSeriesVisible="{Binding MxChecked}"/>
<syncfusion:FastLineBitmapSeries ItemsSource="{Binding FzCircularBuffer}"
XBindingPath="XData" YBindingPath="YData"
StrokeThickness="1" Interior="BlueViolet"
ShowTooltip="False" ShowTrackballInfo="False"
IsSeriesVisible="{Binding FzChecked}"/>
<syncfusion:FastLineBitmapSeries ItemsSource="{Binding MzCircularBuffer}"
XBindingPath="XData" YBindingPath="YData"
StrokeThickness="1" Interior="Blue"
ShowTooltip="False" ShowTrackballInfo="False"
IsSeriesVisible="{Binding MzChecked}"/>
</syncfusion:SfChart.Series>
</syncfusion:SfChart>
</DockPanel>
</Border>
<!--COP BORDER-->
<Border Grid.Row="2"
Grid.Column="3"
Margin="5"
BorderBrush="Black"
BorderThickness="2"
CornerRadius="5"
Padding="10"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<DockPanel>
<Label Content="C O P"
Grid.Column="0" Grid.ColumnSpan="2"
DockPanel.Dock="Top"
HorizontalAlignment="Center"
FontSize="16"
FontWeight="Bold"
Padding="0,0,0,5"/>
<syncfusion:SfChart x:Name="CopChart"
VerticalContentAlignment="Stretch"
HorizontalAlignment="Center"
AreaBorderThickness="5"
AreaBorderBrush="#523B97">
<syncfusion:SfChart.PrimaryAxis>
<syncfusion:NumericalAxis Minimum="-20"
Maximum="20"
Interval="10"
PlotOffset="5"
BorderThickness="0"
TickLineSize="0"
FontSize="12">
<syncfusion:NumericalAxis.AxisLineStyle>
<Style TargetType="Line">
<Setter Property="StrokeThickness" Value="0"/>
</Style>
</syncfusion:NumericalAxis.AxisLineStyle>
</syncfusion:NumericalAxis>
</syncfusion:SfChart.PrimaryAxis>
<syncfusion:SfChart.SecondaryAxis>
<syncfusion:NumericalAxis Minimum="-30"
Maximum="30"
Interval="10"
PlotOffset="5"
BorderThickness="0"
TickLineSize="0"
FontSize="12">
<syncfusion:NumericalAxis.AxisLineStyle>
<Style TargetType="Line">
<Setter Property="StrokeThickness" Value="0"/>
</Style>
</syncfusion:NumericalAxis.AxisLineStyle>
</syncfusion:NumericalAxis>
</syncfusion:SfChart.SecondaryAxis>
<syncfusion:SfChart.Annotations>
<syncfusion:LineAnnotation X1="00" X2="00" Y1="-30" Y2="30"
Stroke="DimGray" StrokeThickness="2" StrokeDashArray="4"/>
<syncfusion:LineAnnotation X1="-20" X2="20" Y1="00" Y2="0"
Stroke="DimGray" StrokeThickness="2" StrokeDashArray="4"/>
</syncfusion:SfChart.Annotations>
<syncfusion:SfChart.Series>
<syncfusion:FastScatterBitmapSeries ItemsSource="{Binding COP_DOT}"
XBindingPath="XData" YBindingPath="YData"
Interior="Red" ScatterHeight="20" ScatterWidth="20"/>
</syncfusion:SfChart.Series>
</syncfusion:SfChart>
</DockPanel>
</Border>
<!--SERIAL BORDER-->
<Border Grid.Row="1"
Grid.Column="3"
Margin="5"
BorderBrush="Black"
BorderThickness="2"
CornerRadius="5"
Padding="15,0,15,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Conexão"
Grid.Column="0" Grid.ColumnSpan="2"
DockPanel.Dock="Top"
HorizontalAlignment="Center"
FontSize="16"
FontWeight="Bold"
Padding="0,0,0,5"/>
<DockPanel Grid.Row="1" Grid.ColumnSpan="2" LastChildFill="True">
<Label Content="Porta COM: "></Label>
<ComboBox ItemsSource="{Binding Available_COMPorts}"
SelectedValue="{Binding SelectedCOMPort, Mode=TwoWay}"
Margin="0,0,0,15" />
<!--SelectedItem="{Binding SelectedCOMPort, Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}"-->
</DockPanel>
<Button Grid.Row="2" Grid.ColumnSpan="2" Content="Connect/Disconnect" HorizontalAlignment="Stretch" Margin="5"
Command="{Binding Path=ConnectButtonCommand, Source={StaticResource LIVviewModel}}"/>
<StackPanel Orientation="Horizontal" Grid.Row="3" Grid.ColumnSpan="2">
<Label Content="Status: "></Label>
<Label Content="{Binding SelectedCOMPort}" Foreground="Red" />
</StackPanel>
</Grid>
</Border>
<Grid Grid.Row="3"
Grid.Column="1" Grid.ColumnSpan="3"
HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!--SERIAL BORDER-->
<Border Grid.Column="0"
Margin="5"
BorderBrush="Black"
BorderThickness="2"
CornerRadius="5"
Padding="20,0,20,0">
</Border>
<!--RECORD BORDER-->
<Border Grid.Column="1"
Margin="5"
BorderBrush="Black"
BorderThickness="2"
CornerRadius="5"
Padding="20,0,20,0">
<Grid Margin="5,10,5,10" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1.5*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.ColumnSpan="2" Margin="0,0,0,10"
Content="Click me" Command="{Binding Path=RecordButtonCommand, Source={StaticResource LIVviewModel}}" />
<Label Content="Record Time (s):"
Grid.Column="0" Grid.Row="1" VerticalAlignment="Center"/>
<TextBlock Text="30" Background="White"
Grid.Column="1" Grid.Row="1"
VerticalAlignment="Center"/>
<Label Content="Name:"
Grid.Column="0" Grid.Row="2" VerticalAlignment="Center"/>
<TextBlock Text="Subject Name"
Grid.Column="1" Grid.Row="2" VerticalAlignment="Center"/>
</Grid>
</Border>
<!--Informações do Gráfico BORDER-->
<Border Grid.Column="2"
Margin="5"
BorderBrush="Black"
BorderThickness="2"
CornerRadius="5"
Padding="10,0,10,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Informações do Gráfico"
Grid.Column="0" Grid.ColumnSpan="6"
HorizontalAlignment="Center"
FontSize="16" FontWeight="Bold" />
<CheckBox Content="Fy"
Grid.Column="0" Grid.Row="1"
FontWeight="ExtraBlack" FontSize="12"
Foreground="DarkGreen"
HorizontalAlignment="Center"
IsChecked="{Binding FyChecked}" />
<CheckBox Content="My"
Grid.Column="1" Grid.Row="1"
FontWeight="ExtraBlack" FontSize="12"
Foreground="LimeGreen"
HorizontalAlignment="Center"
IsChecked="{Binding MyChecked}" />
<CheckBox Content="Fx"
Grid.Column="2" Grid.Row="1"
FontWeight="ExtraBlack" FontSize="12"
Foreground="IndianRed"
HorizontalAlignment="Center"
IsChecked="{Binding FxChecked}" />
<CheckBox Content="Mx"
Grid.Column="3" Grid.Row="1"
FontWeight="ExtraBlack" FontSize="12"
Foreground="Red"
HorizontalAlignment="Center"
IsChecked="{Binding MxChecked}" />
<CheckBox Content="Fz"
Grid.Column="4" Grid.Row="1"
FontWeight="ExtraBlack" FontSize="12"
Foreground="BlueViolet"
HorizontalAlignment="Center"
IsChecked="{Binding FzChecked}" />
<CheckBox Content="Mz"
Grid.Column="5" Grid.Row="1"
FontWeight="ExtraBlack" FontSize="12"
Foreground="Blue"
HorizontalAlignment="Center"
IsChecked="{Binding MzChecked}" />
<DockPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" LastChildFill="True" HorizontalAlignment="Stretch">
<Label Content="Periodo: " VerticalAlignment="Center"/>
<ComboBox Text="30" Background="White" VerticalAlignment="Center"
SelectedIndex="{Binding PeriodSelectedIndex}"> <!--5-->
<ComboBoxItem Content="100 ms" />
<ComboBoxItem Content="500 ms" />
<ComboBoxItem Content="1 s" />
<ComboBoxItem Content="5 s" />
<ComboBoxItem Content="10 s" />
<ComboBoxItem Content="30 s" />
</ComboBox>
</DockPanel>
<DockPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" LastChildFill="True" HorizontalAlignment="Stretch">
<Label Content="Amplitude: " VerticalAlignment="Center"/>
<ComboBox Text="30" Background="White" VerticalAlignment="Center"
SelectedIndex="{Binding AmplitudeSelectedIndex}"> <!--3-->
<ComboBoxItem Content="10"/>
<ComboBoxItem Content="100"/>
<ComboBoxItem Content="500"/>
<ComboBoxItem Content="1000"/>
<ComboBoxItem Content="5000"/>
<ComboBoxItem Content="10000"/>
</ComboBox>
</DockPanel>
<Label Grid.Row="2" Grid.Column="3" Grid.ColumnSpan="3" Content="{Binding PesoKg}" ContentStringFormat="Peso (kg): {0}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Label Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="3" Content="{Binding PesoNw}" ContentStringFormat="Força (N): {0}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</Border>
</Grid>
</Grid>
</UserControl>
Connect Button
public class ConnectButtonCommand : ICommand
{
public LiveGraphViewModel ViewModel { get; set; }
public ConnectButtonCommand(LiveGraphViewModel viewModel)
{
this.ViewModel = viewModel;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
this.ViewModel.ConnectButton();
}
}
DataContext
public partial class LiveGraphView : UserControl
{
private LiveGraphViewModel _vm;
public LiveGraphView()
{
InitializeComponent();
this._vm = new LiveGraphViewModel();
this.DataContext = this._vm;
}
}
As mentioned by #Clemens I had two view model instances at the same time.
One created in the UserControl's constructor (code behind) and other on the XAML Resources.
So, I removed the last one and everything works fine.
<!--
<UserControl.Resources>
<viewmodels:LiveGraphViewModel x:Key="LIVviewModel"/>
</UserControl.Resources>
-->
<Label Content="Porta COM: "></Label>
<ComboBox ItemsSource="{Binding Available_COMPorts}"
SelectedValue="{Binding SelectedCOMPort, Mode=TwoWay}" />
<Button Content="Connect/Disconnect"
Command="{Binding Path=ConnectButtonCommand}" />

Two ListBoxes with two (almost similar) templates

I have 2 Listboxes with 2 data templates that are almost identical except that one of them contains TextBox instead of ComboBox.
First Template :
<DataTemplate x:Key="OldPanelsTemplate" DataType="{x:Type VM:CustomPanelBoard}">
<Grid Height="60" Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Height="60" Grid.RowSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="Blue" BorderThickness="1" Width="35" Background="Blue"
Margin="0 0 2 0" >
<TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Blue"
Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}, Path=(ItemsControl.AlternationIndex),
Converter={StaticResource IncrementerConverter}}" />
</Border>
<TextBlock Grid.Column="1" Text="{Binding Name}" />
<TextBlock Grid.Column="2" Text="{Binding DistributionSystemName}"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="Number of circuits to be copied: "/>
<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding NumberOfCircuitsToBeCopied}" />
</Grid>
</DataTemplate>
Second Template :
<DataTemplate x:Key="NewPanelsTemplate" DataType="{x:Type VM:CustomPanelBoard}">
<Grid Height="60">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Height="60" Grid.RowSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="Blue" BorderThickness="1" Width="35" Background="Blue"
Margin="0 0 2 0" >
<TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Blue"
Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}, Path=(ItemsControl.AlternationIndex),
Converter={StaticResource IncrementerConverter}}" />
</Border>
<TextBlock Grid.Column="1" Text="{Binding Name}" />
<ComboBox Grid.Column="2" ItemsSource="{Binding ValidDistributionSystemsForPanel}"
SelectedItem="{Binding SelectedValidDistributionSystemsForPanel}" HorizontalAlignment="Stretch"
IsHitTestVisible="{Binding DistributionSystemNotAssigned}" IsEnabled="{Binding DistributionSystemNotAssigned}"
ItemTemplate="{StaticResource DistributionSystemTemplate}" >
</ComboBox>
<TextBlock Grid.Row="1" Grid.Column="1" Text="Number of available ways: "/>
<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding NumberOfAvailableWays}" />
</Grid>
</DataTemplate>
As you can see they are both almost identical except for this part :
<ComboBox Grid.Column="2" ItemsSource="{Binding ValidDistributionSystemsForPanel}"
SelectedItem="{Binding SelectedValidDistributionSystemsForPanel}" HorizontalAlignment="Stretch"
IsHitTestVisible="{Binding DistributionSystemNotAssigned}" IsEnabled="{Binding DistributionSystemNotAssigned}"
ItemTemplate="{StaticResource DistributionSystemTemplate}" >
</ComboBox>
The problem is whenever i change anything in one of them i have to change the same thing in the other also ... Any way that can merge them in some way and make the combobox the only variable that changes according to which listbox is calling the template ?
Here an attempt on how you can achieve that, but I have to admit that it is messy but answers your question blindly! a better approach would be to properly implement a DataTemplateSelector.
The idea is to decouple the changing parts into two separate DataTemplates and put them into the resources, one with the Combobox and one for the TextBlock in your case:
<DataTemplate x:Key="DataTemplateCombobox">
<ComboBox ItemsSource="{Binding ValidDistributionSystemsForPanel}" ...>
</ComboBox>
</DataTemplate>
<DataTemplate x:Key="DataTemplateTextblock" >
<TextBlock Text="{Binding DistributionSystemName}" ... />
</DataTemplate>
Now those controls will be replaced with a ContentPresenter in your main (common) DataTemplate. The ContentPresenter uses a ContentTemplateSelector to select which sub-DataTemplate to used based on the name of the ListBox on which this DataTemplate is applied, this is why the Content is bound directly o the ListBox using ancestry binding:
<local:ValueDataTemplateSelector x:Key="TemplateSelector"
DefaultDataTemplate="{StaticResource DataTemplateTextblock}"
ComboboxDataTemplate="{StaticResource DataTemplateCombobox}"
TextBlockDataTemplate="{StaticResource DataTemplateTextblock}" />
<DataTemplate x:Key="OldPanelsTemplate">
<Grid Height="60" Margin="0" Name="OldPanelsTemplateGrid" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Height="60" Grid.RowSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="Blue" BorderThickness="1" Width="35" Background="Blue"
Margin="0 0 2 0" >
<TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Blue" Text="tex" />
</Border>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}" />
<ContentPresenter ContentTemplateSelector="{StaticResource TemplateSelector}" Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" Grid.Row="0" Grid.Column="2">
</ContentPresenter>
<TextBlock Grid.Row="1" Grid.Column="1" Text="Number of circuits to be copied: "/>
<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding NumberOfCircuitsToBeCopied}" />
</Grid>
</DataTemplate>
And here how your DataTemplateSelector should be implemented (basically):
public class ValueDataTemplateSelector : DataTemplateSelector
{
public DataTemplate DefaultDataTemplate { get; set; }
public DataTemplate ComboboxDataTemplate { get; set; }
public DataTemplate TextBlockDataTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var lb = item as ListBox;
if (lb is null)
return DefaultDataTemplate;
if (lb.Name == "ListOne")
return ComboboxDataTemplate;
if (lb.Name == "ListTwo")
return TextBlockDataTemplate;
return DefaultDataTemplate;
}
}
Finally, since your sub-DataTemplates lose their DataContexts due to the Content of the ContentPresenter being bound to the ListBox directly, then just hook their DataContexts again using ElementName Binding or something:
<DataTemplate x:Key="DataTemplateCombobox">
<ComboBox DataContext="{Binding ElementName=OldPanelsTemplateGrid, Path=DataContext}" ItemsSource="{Binding ValidDistributionSystemsForPanel}" >
</ComboBox>
</DataTemplate>
<DataTemplate x:Key="DataTemplateTextblock" >
<TextBlock Text="{Binding DistributionSystemName}" DataContext="{Binding ElementName=OldPanelsTemplateGrid, Path=DataContext}"/>
</DataTemplate>
OldPanelsTemplateGrid is the First Grid in the main DataTemplate that should have the valid ListBoxItem DataContext.
Here is the full Xaml code:
</Window ...
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="450" Width="800" >
<Window.Resources>
<DataTemplate x:Key="DataTemplateCombobox">
<ComboBox DataContext="{Binding ElementName=OldPanelsTemplateGrid, Path=DataContext}" ItemsSource="{Binding ValidDistributionSystemsForPanel}" >
</ComboBox>
</DataTemplate>
<DataTemplate x:Key="DataTemplateTextblock" >
<TextBlock Text="{Binding DistributionSystemName}" DataContext="{Binding ElementName=OldPanelsTemplateGrid, Path=DataContext}"/>
</DataTemplate>
<local:ValueDataTemplateSelector x:Key="TemplateSelector"
DefaultDataTemplate="{StaticResource DataTemplateTextblock}"
ComboboxDataTemplate="{StaticResource DataTemplateCombobox}"
TextBlockDataTemplate="{StaticResource DataTemplateTextblock}" />
<DataTemplate x:Key="OldPanelsTemplate">
<Grid Height="60" Margin="0" Name="OldPanelsTemplateGrid" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Height="60" Grid.RowSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="Blue" BorderThickness="1" Width="35" Background="Blue"
Margin="0 0 2 0" >
<TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Blue" Text="tex" />
</Border>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}" />
<ContentPresenter ContentTemplateSelector="{StaticResource TemplateSelector}" Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" Grid.Row="0" Grid.Column="2">
</ContentPresenter>
<TextBlock Grid.Row="1" Grid.Column="1" Text="Number of circuits to be copied: "/>
<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding NumberOfCircuitsToBeCopied}" />
</Grid>
</DataTemplate>
</Window.Resources>
<!--DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=2,AncestorType=DataTemplate}}"-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ListBox x:Name ="ListOne" ItemsSource="{Binding MyCollection}" ItemTemplate="{StaticResource OldPanelsTemplate}"/>
<ListBox x:Name ="LisTwo" ItemsSource="{Binding MyCollection}" ItemTemplate="{StaticResource OldPanelsTemplate}" Grid.Row="1"/>
</Grid>
I managed to solve it by merging them in one template like this:
<DataTemplate x:Key="NewOldPanelsTemplate" DataType="{x:Type VM:CustomPanelBoard}">
<Grid Height="60">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Height="60" Grid.RowSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="Blue" BorderThickness="1" Width="35" Background="Blue"
Margin="0 0 2 0" >
<TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Blue"
Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}, Path=(ItemsControl.AlternationIndex),
Converter={StaticResource IncrementerConverter}}" />
</Border>
<TextBlock Grid.Column="1" Text="{Binding Name}" />
<!-- To have same template for new and old panels we had the two elements (combobox and textblock) for distribution system and toggle visibility by converters according to their groupbox title -->
<ComboBox Grid.Column="2" ItemsSource="{Binding ValidDistributionSystemsForPanel}"
SelectedItem="{Binding SelectedValidDistributionSystemsForPanel}" HorizontalAlignment="Stretch"
IsHitTestVisible="{Binding DistributionSystemNotAssigned}" IsEnabled="{Binding DistributionSystemNotAssigned}"
ItemTemplate="{StaticResource DistributionSystemTemplate}"
Visibility="{Binding RelativeSource={RelativeSource AncestorType=GroupBox, Mode=FindAncestor}, Path=Header,Converter={StaticResource NewPanelsTemplateVisibilityConverter}}">
</ComboBox>
<TextBlock Grid.Column="2" Text="{Binding DistributionSystemName}"
Visibility="{Binding RelativeSource={RelativeSource AncestorType=GroupBox, Mode=FindAncestor}, Path=Header,Converter={StaticResource OldPanelsTemplateVisibilityConverter}}"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="Number of available ways: "/>
<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding NumberOfAvailableWays}" />
</Grid>
</DataTemplate>
and control the visibility of variable parts(ComboBox and TextBlock in my case) with converters like this :
public class OldPanelsTemplateVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((string)value == "Old Panels")
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
}
public class NewPanelsTemplateVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((string)value == "Old Panels")
{
return Visibility.Collapsed;
}
return Visibility.Visible;
}}
The solution above is based on that each of my listboxes is surrounded by groupbox with certain header (In other cases you may want to use ListBox Names in the converter as visibility toggle)
Thanks to #PavelAnikhouski for the idea of converters.
Also i tried the solution of DataTemplateSelector proposed by #SamTheDev and it worked also (Thanks #SamTheDev), but i preferred the converter solution because i didn't feel comfortable with the idea of losing datacontext through using content presenter (The bottom line here is both solutions work and no one is more elegant it is just personal preference)

Silverlight : How to access control in DataGrid RowDetailTemplate

I have below code and I wanted to get the "btnUpdate" button control in the RowDetailsTemplate and set the visibility in code behind. How can I do that? Any advice is appreciated and thanks in advance.
<Grid x:Name="LayoutRoot" Background="White">
<Grid HorizontalAlignment="Left" Name="grid1" VerticalAlignment="Top" >
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
<Grid HorizontalAlignment="Left" Name="grid2" VerticalAlignment="Top" Grid.RowSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="500" />
<ColumnDefinition Width="80*" />
<ColumnDefinition Width="111*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Content="Refresh" Click="btnRefresh_Click" Grid.Column="1" Height="23" HorizontalAlignment="Right" Name="btnRefresh" VerticalAlignment="Top" Width="80"/>
<Button Content="Add" Click="btnAdd_Click" Grid.Column="2" Height="23" HorizontalAlignment="Right" Name="btnInsert" VerticalAlignment="Top" Width="80" />
<sdk:Label Height="28" HorizontalAlignment="Left" Name="Title" VerticalAlignment="Top" Content="Title" HorizontalContentAlignment="Left" FontSize="20" FontWeight="Bold" Width="Auto"/>
</Grid>
<data:DataGrid x:Name="OverviewDataGrid" Grid.Row="1" AutoGenerateColumns="False" RowEditEnded="OverviewDataGrid_RowEditEnded" SelectionChanged="OverviewDataGrid_SelectionChanged">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Code" Binding="{Binding code, TargetNullValue=(enter code)}" IsReadOnly="True"></data:DataGridTextColumn>
<data:DataGridTextColumn Header="Description" Binding="{Binding description}" IsReadOnly="True"></data:DataGridTextColumn>
<data:DataGridTemplateColumn Header="Delete">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btnDelete" Content="Delete" Click="btnDelete_Click"></Button>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn >
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btnCollapse" Grid.Column="0" Grid.Row="0" Content="+" Click="btnCollapse_Click" HorizontalAlignment="Center" Width="20" VerticalAlignment="Center" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
<data:DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border BorderThickness="0" Background="BlanchedAlmond" Padding="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.17*"/>
<ColumnDefinition Width="0.30*"/>
<ColumnDefinition Width="0.17*"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock FontSize="12" Text="Code : " VerticalAlignment="Center" Grid.Column="0" Grid.Row="0" Margin="1"/>
<TextBlock FontSize="12" FontWeight="Bold" Foreground="MidnightBlue" Text="{Binding code}" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="0" Margin="1" Grid.ColumnSpan="3"/>
<TextBlock FontSize="12" Text="Description: " VerticalAlignment="Center" Grid.Column="0" Grid.Row="1" Margin="1"/>
<TextBox FontSize="12" Text="{Binding description, Mode=TwoWay}" Width="350" TextWrapping="NoWrap" HorizontalAlignment="Left"
AcceptsReturn="True" Grid.Column="1" Grid.Row="1" Margin="1" Grid.ColumnSpan="3"/>
<Button x:Name="btnUpdate1" Content="Update" Click="btnUpdate_Click" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="4" Margin="1"/>
</Grid>
</Border>
</DataTemplate>
</data:DataGrid.RowDetailsTemplate>
</data:DataGrid>
</Grid>
</Grid>
Follow below steps to get the control in RowDetailsTemplate.
Add loadingRowDetails event {LoadingRowDetails="yourDataGrid_LoadingRowDetails"} into datagrid in xaml.
Add below code at xaml class
private void yourDataGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
Button btn = (e.DetailsElement as FrameworkElement).FindName("btnUpdate") as Button;
if (ok.Equals(false))
btn.Visibility = System.Windows.Visibility.Collapsed;
}

Silverlight, XAML, DataGrid does not autosize, no auto scrollbars

Can anyone tell me why the datagrid in this example gets cut off when it grows past the bounds of the Grid.Row which contains it? Here is the xaml and code-behind which you can use in a VS 2010 'Silverlight Application' template. Thanks in advance.
<UserControl
x:Class="SilverlightApplication3.MainPage"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
mc:Ignorable="d" >
<Grid>
<Border>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- header -->
<Border Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="40" >
</Border>
<!-- employee category selection -->
<Grid Grid.Row="1" Margin="10">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Category:" Margin="0,0,10,0" VerticalAlignment="Center" />
<ComboBox ItemsSource="{Binding EmployeeTypes}" SelectedItem="{Binding EmployeeType, Mode=TwoWay}" MinWidth="100" />
</StackPanel>
<Border BorderBrush="Black" BorderThickness="10" Height="2" Margin="0,10,0,0" ></Border>
</StackPanel>
</Grid>
<!-- content -->
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- daily employee grid -->
<Grid Grid.Row="0" Visibility="Visible" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Text="Category Type:" />
<ComboBox ItemsSource="{Binding Categories}"
SelectedItem="{Binding Category, Mode=TwoWay}"
DisplayMemberPath="Name" HorizontalAlignment="Left" Width="250">
</ComboBox>
<TextBlock Text="Category Types:" Margin="0,10,0,0" />
<sdk:DataGrid x:Name="dataGrid" AutoGenerateColumns="True"
HorizontalAlignment="Left" VerticalAlignment="Stretch" MinWidth="250" VerticalScrollBarVisibility="Visible" >
<sdk:DataGrid.ColumnHeaderStyle>
<Style TargetType="sdk:DataGridColumnHeader">
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</sdk:DataGrid.ColumnHeaderStyle>
</sdk:DataGrid>
</StackPanel>
</Grid>
</Grid>
<!-- buttons -->
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0" >
<Button Command="{Binding SaveCommand}"
Width="80" HorizontalContentAlignment="Center" Margin="0,0,2,0">
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Approve24x24.png" Height="24" Width="24"/>
<TextBlock Text="Save" VerticalAlignment="Center" Margin="2"/>
</StackPanel>
</Button>
<Button Command="{Binding CancelCommand}"
Width="80" HorizontalContentAlignment="Center">
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Delete24x24.png" Height="24" Width="24"/>
<TextBlock Text="Cancel" VerticalAlignment="Center" Margin="2"/>
</StackPanel>
</Button>
</StackPanel>
</Grid>
</Border>
</Grid>
and the code-behind:
using System.Collections.Generic;
using System.Windows.Controls;
namespace SilverlightApplication3
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
List<string> testItems = new List<string>();
for (int i = 0; i < 50; i++)
{
testItems.Add(string.Format("Item Number {0}", i.ToString()));
}
this.dataGrid.ItemsSource = testItems;
}
}
}
You used the StackPanel to display some elements and datagrid. So here datagrid is not constrained to any height, moreover the the parent grid of stackpanel's height is Auto. So there is no where you are making grid to occupy fixed size.
I modified your code with in a grid.
<!-- content -->
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- daily employee grid -->
<Grid Grid.Row="0" Visibility="Visible" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="28" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel>
<TextBlock Text="Category Type:" />
<ComboBox ItemsSource="{Binding Categories}"
SelectedItem="{Binding Category, Mode=TwoWay}"
DisplayMemberPath="Name" HorizontalAlignment="Left" Width="250">
</ComboBox>
</StackPanel>
<TextBlock Text="Category Types:" Margin="0,10,0,0" Grid.Row="1" />
<sdk:DataGrid x:Name="dataGrid" AutoGenerateColumns="True" Grid.Row="2" ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="10"
HorizontalAlignment="Left" VerticalAlignment="Top" MinWidth="250" VerticalScrollBarVisibility="Visible" >
<sdk:DataGrid.ColumnHeaderStyle>
<Style TargetType="sdk:DataGridColumnHeader">
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</sdk:DataGrid.ColumnHeaderStyle>
</sdk:DataGrid>
</Grid>
</Grid>
This should give you the expected result.

Create a Master/Details View based on TreeView Control in Silverlight

I have a TreeNode class with following properties
public string Name { get; set; }
public string Description { get; set; }
public bool AllowMultiples { get; set; }
private ObservableCollection<TreeNode> _childNodes = new ObservableCollection<TreeNode>();
In my UI I want to display tree in left panel and selected items details in right side.
My XAML looks like following:
<UserControl.Resources>
<win:HierarchicalDataTemplate x:Key="ChildTemplate" ItemsSource="{Binding ChildNodes}">
<TextBlock FontStyle="Italic" Text="{Binding Path=Name}" />
</win:HierarchicalDataTemplate>
<win:HierarchicalDataTemplate x:Key="NameTemplate"
ItemsSource="{Binding Path=ChildNodes}"
ItemTemplate="{StaticResource ChildTemplate}">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" />
</win:HierarchicalDataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="Document Hierarchy" Margin="0,0,43,0" FontSize="13" />
<toolkit:TreeViewDragDropTarget Grid.Row="1" Grid.Column="0" BorderThickness="-1">
<controls:TreeView BorderThickness="0" ItemTemplate="{StaticResource ChildTemplate}" x:Name="treeView" ItemsSource="{Binding}">
</controls:TreeView>
</toolkit:TreeViewDragDropTarget>
<TextBlock Text="Properties" FontSize="11" Grid.Column="1" Grid.Row="0" />
<Grid Grid.Row="1" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" ></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock x:Name="AlloMultPropLabel" Text="Allow Multiple" Grid.Column="0" Grid.Row="0"></TextBlock>
<RadioButton x:Name="AllowMulti" GroupName="GrpAllowMulti" Content="True" Grid.Column="1" Grid.Row="0"/>
<RadioButton x:Name="AllowMulti2" GroupName="GrpAllowMulti" Content="False" Grid.Column="2" Grid.Row="0" IsChecked="True" />
<TextBlock x:Name="DescPropLabel" Text="Description" HorizontalAlignment="Left" Width="74" Grid.Row="2" ></TextBlock>
<TextBox x:Name="DescProp" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" Text="{Binding Description}" />
</Grid>
Using http://www.silverlight.net/learn/quickstarts/bindingtocontrols/ as my reference I have set the LayoutRoot.DataContext to CollectionViewSource as below:
public ObservableCollection<TreeNode> itemsSource = new ObservableCollection<TreeNode>()
{
new TreeNode("Root", "ths is test"),
new TreeNode("Secondary","Second node"),
};
public MainPage()
{
InitializeComponent();
//treeView.DataContext = itemsSource;
LayoutRoot.DataContext = new CollectionViewSource { Source = itemsSource };
}
When I run this project I notice that the description is always set to first items description. It is not getting changed based on selection.
Any pointers to get this right?
You have bound the TextBox to "{Binding Description}". It will using the same DataContext that the TreeView starts with hence it shows the description of the top item.
In order to for the textbox to follow the selected item in the tree its data context needs to be bound to the SelectedItem of the Treeview. I'd be inclined to placing the "Detail" display in its own Grid and manage its layout in there, this would be a good place to update the DataContext:-
<Grid DataContext="{Binding SelectedItem, ElementName=treeView}">
<Grid.ColumnDefinitions>
<Grid.ColumnDefinition Width="Auto" />
<Grid.ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<Grid.RowDefinition Height="Auto" />
<Grid.RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Name" Grid.Row="0" Grid.Column="0" />
<TextBox Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="Description" Grid.Row="1" Grid.Column="0" />
<TextBox Text="{Binding Description}" Grid.Row="1" Grid.Column="1"/>
</Grid>
Note the binding on the Grid DataContext it uses the treeView as the source object and binds to the SelectedItem.

Resources