Silverlight WCF databind related tables - silverlight

I have a table with foreign keys. I want to display the data as the description and not the Id for the related table. From the Entity service I have made the .Include statement on the data model for it to return. Here is what I have tried, the station shows up in the cell, but nothing in the combobox:
<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False" Width="Auto" Header="Station">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Station.Description}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Station}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<OperationContract()>
Public Function LoadPersonnel(clientId As Integer) As List(Of Personnel)
Dim result As List(Of Personnel)
Try
result = db.Personnels.Include("Station").Where(Function(o) o.ClientId = clientId).ToList
Catch
result = Nothing
End Try
Return result
End Function
Also tried:
<ComboBox ItemsSource="{Binding}" DisplayMemberPath="{Binding Station.Description}" SelectedItem="{Binding Station.Description}" SelectedValue="{Binding Station.StationId}" />

Do like this,
<sdk:DataGrid AutoGenerateColumns="False" Height="100" HorizontalAlignment="Left" Margin="107,90,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="251" >
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False" Width="Auto" Header="Station">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Station.Description}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Station}" DropDownOpened="ComboBox_DropDownOpened" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
in the drop down opened event, assign the itemssource,
private void ComboBox_DropDownOpened(object sender, EventArgs e)
{
var combo = (ComboBox)sender;
combo.ItemsSource = Station;
combo.DisplayMemberPath = "Description";
}

Related

Binding RichTextBox inside of dataGrid

So I have this WPF app to pull data from database and display in dataGrid:
This app store rtf format code in [Content]. I want it to convert every listing under [Content] and show it in the richtextbox inside of my dataGrid.
My XAML:
<DataGrid x:Name="dataGrid" ColumnWidth="*" HorizontalAlignment="Left" VerticalAlignment="Top" Height="512" Width="774" SelectionChanged="dataGrid_SelectionChanged" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Title}" ClipboardContentBinding="{x:Null}" Header="Ttile" Width="100" FontFamily="Segoe UI Semibold"/>
<DataGridTemplateColumn ClipboardContentBinding="{x:Null}" Header="Content" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RichTextBox x:Name="rtb">
<FlowDocument Name="rtbFlowDoc" PageWidth="{Binding ElementName=rtb, Path=ActualWidth}">
<Paragraph>
<Run Text="{Binding Content}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn ClipboardContentBinding="{x:Null}" Header="Image">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Files}" Width="300" Height="300" ScrollViewer.HorizontalScrollBarVisibility="Auto"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
My C# for converting rtf to content(how do you auto convert each datarow btw?):
string rtfText = text;
byte[] byteArray = Encoding.ASCII.GetBytes(rtfText);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray))
{
System.Windows.Documents.TextRange tr = new System.Windows.Documents.TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
tr.Load(ms, System.Windows.DataFormats.Rtf);
}

Showing Hiding a control inside datagrid in silverlight with MVVM

I have a datagrid like below
**strong text**
<sdk:DataGrid AutoGenerateColumns="False" Height="Auto" MaxHeight="500"
HorizontalAlignment="Left" Margin="5" Name="_EmployeeGrid"
ItemsSource="{Binding Path= Employees,Mode=TwoWay}"
HorizontalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Visibility="Visible"
VerticalAlignment="Top" Width="Auto">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Employee ID" IsReadOnly="True" Binding="{Binding Path=EmpID}"/>
<sdk:DataGridTemplateColumn Header="Name" SortMemberPath="EmpID">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<HyperlinkButton VerticalAlignment="Center" NavigateUri="{Binding Path=EmpID,
Converter={StaticResource NavigatePropertyUriConverter}, ConverterParameter=EmpView}"
Content= "{Binding Path=EmpID}" >
</HyperlinkButton>
<!--<HyperlinkButton NavigateUri= "{ Binding Converter={StaticResource navigateConv}, ConverterParameter=/Property/IssuesView?}" VerticalAlignment="Center"
Content="{Binding Path=PropertyId}"
HorizontalAlignment="Right" Margin="5"/>-->
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<!--<sdk:DataGridTextColumn Header="Property #" IsReadOnly="True" Binding="{Binding Path=PropertyId}"/>-->
<sdk:DataGridTextColumn Header="Address" IsReadOnly="True" Binding="{Binding Path=Address}"/>
<sdk:DataGridTextColumn Header="Category" IsReadOnly="True" Binding="{Binding Path=CategoryName}"/>
<sdk:DataGridTextColumn Header="Phone" IsReadOnly="True" Binding="{Binding Path=UnitId}" Visibility="Collapsed"/>
<!--<sdk:DataGridTextColumn Header="Notes" Binding="{Binding Comments}" IsReadOnly="False" />-->
<sdk:DataGridTemplateColumn x:Name="Notes1" Header="Notes">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock MaxWidth="200" TextAlignment="Left" TextWrapping= "Wrap" Text="{Binding Path=Comments, Mode=OneWay}" Width="200" />
<HyperlinkButton Name="btnEllipsis" Visibility="{Binding EllipsisVisibility,Source={StaticResource ViewModel}}" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="15" Content="..." ToolTipService.ToolTip="Click For More" Command="{Binding NotesCommand,Source={StaticResource ViewModel}}" CommandParameter="{Binding}" >
</HyperlinkButton>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid>
and associated property in viewmodel as below
strong text
string _ellipsisVisibility;
public string EllipsisVisibility
{
get { return _ellipsisVisibility; }
set
{
if (_ellipsisVisibility != value)
{
_ellipsisVisibility = value;
RaisePropertyChanged("EllipsisVisibility");
}
}
}
and setting it to
EllipsisVisibility="Collapsed";
and
EllipsisVisibility="Visible";
There are two questions
1. It is not working to show or hide column
2. Need to show the hyperlink button in few rows and or hide in few rows based on some condition like Rowdatabound event in asp.net.
How to do can anyone please help???
(1) DataGrid column visibility is, unfortunately, not a dependency property. This means it can't engage in databinding. Some work-arounds to this involve subclassing the column type you're interested in.
(2) To show/hide a particular control, based on the data for that row, I would use a converter. Or create a new property on that entity type to directly bind to.

WPF DataGrid takes long time to display data after ItemSource is updated

I tried to load the datagrid based on the selection of two combo boxes. When a combo box item selected it updates the SelectedAccounts property which is set as ItemSource for datagrid binding. I checked that ItemSource is updated quickly but WPF DataGrid takes long time to display data(~ 2500 rows). Please suggest me to improve loading performance.
<DataGrid Name="dgSelecteAccounts" Grid.Row="2" Grid.Column="2" ItemsSource="{Binding SelectedAccounts}" SelectionMode="Extended" EnableRowVirtualization="True" Margin="0,25,0,0" Grid.RowSpan="2" Width="600">
<DataGrid.Columns>
<DataGridTextColumn Header="Account Code" Binding="{Binding Code}" IsReadOnly="True" Width="100" SortDirection="Ascending" />
<DataGridTextColumn Header="Account Name" Binding="{Binding Name}" IsReadOnly="True" Width="300" />
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal" Grid.ColumnSpan="3" HorizontalAlignment="Left" Width="890" Height="28" Grid.RowSpan="3" VerticalAlignment="Top">
<Label Content="As of Date:" Foreground="Black"/>
<ComboBox Width="135" ItemsSource="{Binding AsOfDates}" SelectedItem="{Binding SelectedPeriod, Mode=TwoWay, UpdateSourceTrigger=Explicit}" util:CancellableSelectionBehavior.SelectionChangeAllowedChecker="{Binding FilterChangedCheck}"/>
<Label Content="{Binding SelectedCategory.Name}" ContentStringFormat=" {0}:" Foreground="Black" Margin="15,0,0,0" />
<ComboBox ItemsSource="{Binding AumCategoryValues}" DisplayMemberPath="Value" SelectedItem="{Binding SelectedCategoryValue, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Width="135" util:CancellableSelectionBehavior.SelectionChangeAllowedChecker="{Binding FilterChangedCheck}" />
</StackPanel>
private ObservableCollection<FirmAumAccount> selectedAccounts;
public ObservableCollection<FirmAumAccount> SelectedAccounts
{
get { return selectedAccounts; }
set
{
selectedAccounts = value;
RaisePropertyChangedEvent("SelectedAccounts");
}
}

Add Row to DataGrid if ItemSource Binding returns no data

is there any simple solution to this problem..first i thought i can create an empty ObservableCollection and writte:
ObservableCollection<PersonDetailsView> newOCollection= new ObservableCollection<PersonDetailsView>();
myDataGrid.ItemsSource=newOCollection.ToArray();
and it works..i get an empty row..but the problem is that i loose the other binding..is there any way to Bind to newOCollection only if my {Binding Person} (see the code) doesn't return a value
<DataGrid CanUserAddRows="True" IsReadOnly="False" BorderBrush="#FFCCCCCC"
GridLinesVisibility="All" AutoGenerateColumns="False"
ItemsSource="{Binding Person}" Background="White" Margin="10,45,0,0"
VerticalAlignment="Top" Height="91" HorizontalAlignment="Left"
HeadersVisibility="None" SelectionMode="Single" Name="dtaPersons" Width="415">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Person.Name, Mode=TwoWay}" Width="Auto"/>
<DataGridTextColumn Binding="{Binding Person.Surname, Mode=TwoWay}" Width="*"/>
<DataGridTemplateColumn Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<Button Style="{DynamicResource ChromelessButtonStyle}" Content="r" FontFamily="Marlett" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
ObservableCollection<PersonDetailsView> newOCollection= new ObservableCollection<PersonDetailsView>();
if (!myDataGrid.Items.Any())
myDataGrid.ItemsSource=newOCollection.ToArray();

WPF databinding a gridview issue

I am working on a dataGrid that is populated from an Entity model. I have the following for the specific column as an attempt to use a combobox during editing and the source for the editing is a CollectionsViewSource.
<Grid DataContext="{StaticResource vsLogins}" Grid.Column="1"
Margin="16,248,154,31">
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True"
Height="213" HorizontalAlignment="Left"
ItemsSource="{Binding Source={StaticResource vsLogins}}"
Name="LoginsDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected"
VerticalAlignment="Top" Width="380"
Background="{StaticResource lgb}" BorderThickness="2"
BorderBrush="#FFFC0303">
<DataGrid.Columns>
<DataGridTextColumn x:Name="LoginNameColumn" Binding="{Binding
Path=LoginName}" Header="Login Name"
Width="200" MinWidth="200" />
<DataGridTextColumn x:Name="PsWrdColumn"
Binding="{Binding Path=PsWrd}"
Header="Password" Width="130" MinWidth="130" />
<DataGridTemplateColumn x:Name="AccessLevelIdColumn" Header="Id"
Width="40" MinWidth="40">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=AccessLevelId}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource
vsAccessLevels}}" DisplayMemberPath="Description"
SelectedValuePath="AccessLevelId" IsEditable="False"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
It is throwing some errors. What am I doing wrong? I also tried putting a grid around the combobox and setting it's dataContent to the CVS - no dice either. Thanks for any help.
<DataGridComboBoxColumn x:Name="AccessIdColumn"
ItemsSource="{Binding Source={StaticResource vsAccessLevels}}"
DisplayMemberPath="Description"
SelectedValuePath="AccessLevelId"
SelectedValueBinding="{Binding Path=AccessLevelId}"/>

Resources