binding to static property problem - c#

I'm using the EntityFramework and working on an invoicing application. Below is the xaml code from myDataGrid. Now it compiles and everything works great, but the problem is, the static binding I have on the DataGridComboBoxColumn throws an exception when in designer mode because that static property actually loads from a MySql database using the entity framework.
<DataGrid x:Name="ItemsGrid" Grid.Row="2" Grid.ColumnSpan="3" ItemsSource="{Binding Items}" FontSize="11" AutoGenerateColumns="False" CanUserAddRows="False" RowHeaderWidth="0" GridLinesVisibility="None" CanUserResizeRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Width="150" Header="Item No.">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="130" BorderThickness="0" Background="Transparent" Text="{Binding Number}" />
<Image MouseDown="ItemSelectionButton_Click" Margin="0,0,0,0" Width="12" Source="/Images/Icons/SearchBlack.png" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="70" Header="Quantity" Binding="{Binding Quantity}" />
<DataGridTextColumn Width="70" Header="Order" Binding="{Binding Order}" />
<DataGridTextColumn Width="70" Header="B/O" Binding="{Binding BackOrder}" />
<DataGridTextColumn Width="60" Header="Units" Binding="{Binding Units}" />
<DataGridTextColumn Width="200" Header="Description" Binding="{Binding Description}" />
<DataGridTextColumn Width="90" Header="Price" Binding="{Binding Price}" />
<DataGridComboBoxColumn Width="50" Header="Tax" ItemsSource="{x:Static app:Session.TaxCodes}" SelectedValueBinding="{Binding TaxCodeID}" DisplayMemberPath="Code" SelectedValuePath="ID" />
<DataGridTextColumn Width="90" Header="Amount" Binding="{Binding Amount}" />
<DataGridTextColumn Width="90" Header="Linked" Binding="{Binding SalesOrderID}" />
</DataGrid.Columns>
</DataGrid>
Here's the exception:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
at DtcInvoicer.Models.DatabaseEntities..ctor() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Models\DatabaseModel.Designer.cs:line 42
at DtcInvoicer.Models.Repository.get_GetContext() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Models\Repository.cs:line 14
at DtcInvoicer.Models.TaxCodesRepository.SelectAll() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Models\TaxCodesRepository.cs:line 54
at DtcInvoicer.Session..cctor() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Session.cs:line 18
I know trying to fix the root of the problem will be too much work, I'm wondering if there's a simple workaround like dynamically binding to a static property in another class. Anyone have any idea how to do this? Looking for a xaml only solution.
Here's what I already tried...
ItemsSource="{Binding Path=TaxCodes, Source={x:Static app:Session}}"
ItemsSource="{Binding Path=TaxCodes, Source={StaticResource app:Session}}"
ItemsSource="{Binding app:Session.TaxCodes}"
Any help is appreciated.
Thanks.

Could you use something like d:ItemsSource="{x:Null}", and define d in your Window or UserControl like mc:Ignorable="d"?
It should make the design-time property of ItemsSource equal to null
Edit
Forgot to add the definition for mc - It is
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

Related

DataGrid not showing in .Droid (Uno-platform)

I made DataGrid that correctly displays data in .Wasm and .UWP, but on .Droid app it doesn't show anything (header or body). The calling to my API works and data is correctly sent to .Droid. I store it in a list and set list as ItemsSource of my DataGrid.
This shows list filling:
list = await ApiService.GetList();
dataGrid1.ItemsSource = list;
UserPage.Xaml:
<Page
...
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<controls:DataGrid
x:Name="dataGrid1" IsReadOnly="True" Width="Auto"
Height="Auto" Margin="12"
AutoGenerateColumns="False"
HorizontalAlignment="Left" VerticalAlignment="Top">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn
Width="SizeToCells"
Binding="{Binding id}"
FontSize="20" />
<controls:DataGridTextColumn
Width="SizeToCells"
Binding="{Binding name}"
FontSize="20" />
<controls:DataGridTextColumn
Width="SizeToCells"
Binding="{Binding role}"
FontSize="20" />
</controls:DataGrid.Columns>
</controls:DataGrid>
I have installed Uno.Microsoft.Toolkit.Uwp.UI.Controls and Uno.Microsoft.Toolkit.Uwp.UI.Controls.DataGrid on .Droid project. I followed documentation but can't seem to find the solution.

Maintain column widths of a DataGrid when a grouping option changes

I've been using Rubberduck for a while now to improve my VBA code. The ducky introduced me to unit tests and lots of other proper techniques. In gratitude I'm trying to give back and contribute a fix to an issue I found, in spite of not knowing C#.
The issue deals with a System.Windows.Controls.Grid. The grid has an option of grouping tests results by their outcome or by the module they reside in. If a column width is adjusted and then the grouping is changed the width change isn't reflected.
I've created properties on the viewmodel that I thought would allow two way binding, one shown below as an example.
private DataGridLength _outcomeColumnWidth;
public DataGridLength OutcomeColumnWidth
{
get => _outcomeColumnWidth;
set
{
_outcomeColumnWidth = value;
OnPropertyChanged();
}
}
I've edited the XAML trying to create a TwoWay binding that will have the changed column width persist after the grouping is changed.
<Grid>
<controls:GroupingGrid ItemsSource="{Binding Source={StaticResource ResultsByOutcome}}"
SelectedItem="{Binding SelectedTest}"
ShowGroupingItemCount="True"
Visibility="{Binding IsChecked, ElementName=GroupByOutcome, Converter={StaticResource BoolToVisibility}}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_Outcome}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="unitTesting:TestMethod">
<Image Source="{Binding Result.Outcome, Converter={StaticResource OutcomeIconConverter}}" Height="16" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_QualifiedModuleName}" Binding="{Binding Declaration.QualifiedName.QualifiedModuleName}"
Width="{Binding OutcomeColumnWidth, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_MethodName}" Binding="{Binding Declaration.IdentifierName}"
Width="{Binding ModuleColumnWidth, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_Message}" Binding="{Binding Result.Output}"
Width="{Binding MessageColumnWidth, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_Duration}" Binding="{Binding Result.Duration, StringFormat={}{0}ms}"
Width="{Binding DurationColumnWidth, Mode=TwoWay}"/>
</DataGrid.Columns>
</controls:GroupingGrid>
<controls:GroupingGrid ItemsSource="{Binding Source={StaticResource ResultsByModule}}"
SelectedItem="{Binding SelectedTest}"
ShowGroupingItemCount="True"
Visibility="{Binding IsChecked, ElementName=GroupByLocation, Converter={StaticResource BoolToVisibility}}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_Outcome}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="unitTesting:TestMethod">
<Image Source="{Binding Result.Outcome, Converter={StaticResource OutcomeIconConverter}}" Height="16" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_QualifiedModuleName}" Binding="{Binding Declaration.QualifiedName.QualifiedModuleName}"
Width="{Binding OutcomeColumnWidth, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_MethodName}" Binding="{Binding Declaration.QualifiedName.MemberName}"
Width="{Binding ModuleColumnWidth, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_Message}" Binding="{Binding Result.Output}"
Width="{Binding MessageColumnWidth, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_Duration}" Binding="{Binding Result.Duration, StringFormat={}{0}ms}"
Width="{Binding DurationColumnWidth, Mode=TwoWay}"/>
</DataGrid.Columns>
</controls:GroupingGrid>
</Grid>
The view model inherits from an abstract class ViewModelBase which implements INotifyPropertyChanged. The abstract class also has public event PropertyChangedEventHandler PropertyChanged and the method OnPropertyChanged. I've set breakpoints on both get and set for the property and neither are ever reached.
DataGrid Columns are not part of the logical or visual tree as they are abstract objects so they don't have access to the DataContext of your ViewModel. Which makes them rather difficult to bind to. Good news is that there are few ways you can do this.
Proxy Object
<UserControl.Resources>
<FrameworkElement x:Key="ProxyElement" DataContext="{Binding}" />
</UserControl.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Descriptions}">
<DataGrid.Columns>
<DataGridTextColumn Header="Description"
Width="{Binding Source={StaticResource ProxyElement}, Path=Width, Mode=TwoWay}"
Binding="{Binding Description}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
For further reading I recommend having a look at https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ as it gives a good overview as to why and gives an alternative to the code above using a specific ProxyClass he creates.

WPF Datagrid does not show entries

Im new to WPF and C# so please dont rip my head of if i should have done something stupid^^
Im trying to show some data from a SQLite Database file by using a DataGrid. I already installed the relevant package from the SQLite page that includes the VisualStudios designer components. I added the Databasefile as a datasource in Visual Studios which worked pretty good. If i open the Database with the datapreview function it shows my Data the way it should.
My Problem is that even tho i boundet the Datasource as shown on the MSDN page it still isnt showing any Entries, but it sucessfully created all Colums.
Heres my XAML Code:
<Page.Resources>
<local:WorkDataSet x:Key="WorkDataSet"/>
<CollectionViewSource x:Key="personalViewSource" Source="{Binding Personal, Source={StaticResource WorkDataSet}}"/>
<Grid DataContext="{StaticResource personalViewSource}">
<Label Content="Personal" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="20" FontSize="20" FontWeight="Bold"/>
<DataGrid x:Name="personalDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="10,100,10,10" RowDetailsVisibilityMode="Visible">
<DataGrid.Columns>
<DataGridTextColumn x:Name="iDColumn" Binding="{Binding ID}" Header="ID" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="stationColumn" Binding="{Binding Station}" Header="Station" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Name" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="_ACD_IDColumn" Binding="{Binding ACD-ID}" Header="ACD-ID" Width="SizeToHeader"/>
<DataGridTemplateColumn x:Name="bild1Column" Header="Bild 1" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Bild1}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="bild2Column" Header="Bild 2" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Bild2}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="visitColumn" Header="Visit" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Visit, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
as far as is understood this it should not require any further C# code cause everything should be set automaticly by Visual Studios.
I know there are a view Questions like this arround here but non of these use SQLite in the way i do...

DataGrid Scrolling not working

I have a datagrid which is populated using data retrieved from a database. Unfortunately, when the number of rows retrieved is more than the height of the datagrid, I cannot scroll vertically or horizontally, even though both scrollbars are shown.
Here is my code:
<data:DataGrid x:Name="DataGrid_Transactions" AutoGenerateColumns="False" VerticalAlignment="Center" Height="300" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Background="#FF0F1111" FontSize="26" Foreground="RoyalBlue" HorizontalGridLinesBrush="AliceBlue" IsReadOnly="True" VerticalGridLinesBrush="AliceBlue" AlternatingRowBackground="AliceBlue" FontFamily="Segoe WP SemiLight" RowDetailsVisibilityMode="Visible" RowBackground="AliceBlue">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Transaction ID" Binding="{Binding TransactionID}" />
<data:DataGridTextColumn Header="Vendor Username" Binding="{Binding VendorUsername}" />
<data:DataGridTextColumn Header="Purchase Description" Binding="{Binding PurchaseDescription}" />
<data:DataGridTextColumn Header="Total Price" Binding="{Binding TotalPrice}" />
<data:DataGridTextColumn Header="Currency" Binding="{Binding Currency}" />
</data:DataGrid.Columns>
</data:DataGrid>
<Button Content="Main Page" Height="72" HorizontalAlignment="Center" Margin="0,540,0,43" Name="Button_MainPage" VerticalAlignment="Center" Width="456" Click="Button_MainPage_Click" />
</Grid>
</Grid>
Can someone please help me? Thank you :)
It seems attached property does not work directly for this type objects,
Even it works for ListBox(http://www.silverlightshow.net/items/Tip-How-to-specify-ScrollViewer-s-attached-properties-in-XAML.aspx)
You should put it in a ScrollViewer
<ScrollViewer ...>
content
</ScrollViewer>
http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer(v=vs.95).aspx
For another way, have a look this attached property coding tip
http://www.codeproject.com/Articles/95746/Exposing-and-Binding-to-a-Silverlight-ScrollViewer
You may also code your own attached property.(you shoud use RegisterAttached while dependency object registration,and obey attached property coding rules.)
I have working code :
<sdk:DataGrid x:Name="dgList" Margin="0" HeadersVisibility="All" AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Single" UseLayoutRounding="True" BorderThickness="1" BorderBrush="#FFA2B4D3">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding DOCTORNO}" Header="No" />
<sdk:DataGridTextColumn Binding="{Binding DOCTORNAME}" Header="Ad Soyad" />
<sdk:DataGridTextColumn Binding="{Binding UZMANLIKKODU}" Header="Branş" />
<sdk:DataGridTextColumn Binding="{Binding BRANSAD}" Header="Branş Adı" />
</sdk:DataGrid.Columns>

Datagrid within a Datagrid, having one of the columns of a Datagrid as a Datagrid itself?

I have the current code that defines my itemsource (what I want to display to the user):
public class View
{
public string Name { get; set; }
public string Quantity { get; set; }
public List<Item> items { get; set; }
}
public class Item
{
public string ItemName { get; set; }
public double ItemQuantity { get; set; }
}
DataSet used:
List<View> myViews
So you end up with data like:
ViewA 50 {ItemA 50, ItemB 10, ItemC 20)
ViewB 10 {ItemC 10, ItemD 10, ItemE 10)
In my datagrid right now it shows the first two columns perfectly but the List shows as "Collection" (not the actual data), which I guess was to be expected.
This is my XAML right now
<DataGrid Grid.Row="2" Margin="0,146,0,350"
ItemsSource="{Binding myViews}"
AutoGenerateColumns="False" Height="104">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="*" FontSize="16"
Binding="{Binding Path=Name}" />
<DataGridTextColumn Header="Quantity" Width="*" FontSize="16"
Binding="{Binding Path=Quantity}" />
<DataGridTextColumn Header="Items" Width="*" FontSize="16"
Binding="{Binding Path=items}" />
</DataGrid.Columns>
</DataGrid>
I am trying to find a way to represent the List items to the user (either in this datagrid or some other way if this is not the right approach).
My idea was to actually have another Datagrid in the 3rd column with its own source of items, but I can't seem to find out how I can make the 3rd column a Datagrid (as opposed to text like it is today). Does anyone know how this can be done (some examples would be awsome).
Or if this is really NOT the right way to solve this problem, any other ideas would also be much appreciated.
Thanks,
You can use RowDetailsTemplate to present list of Item objects.
<DataGrid Grid.Row="2" Margin="0,146,0,350"
ItemsSource="{Binding myViews}"
AutoGenerateColumns="False" Height="104">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding items}">
<DataGrid.Columns>
<DataGridTextColumn Header="Item Name" Width="Auto" FontSize="16"
Binding="{Binding Path=ItemName}" />
<DataGridTextColumn Header="Item Quantity " Width="Auto" FontSize="16"
Binding="{Binding Path=ItemQuantity }" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="*" FontSize="16"
Binding="{Binding Path=Name}" />
<DataGridTextColumn Header="Quantity" Width="*" FontSize="16"
Binding="{Binding Path=Quantity}" />
</DataGrid.Columns>
</DataGrid>
If you want present list of Item objects in third columns you should use DataGridTemplateColumn.
<DataGrid Grid.Row="2" Margin="0,146,0,350"
ItemsSource="{Binding myViews}"
AutoGenerateColumns="False" Height="104">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="*" FontSize="16"
Binding="{Binding Path=Name}" />
<DataGridTextColumn Header="Quantity" Width="*" FontSize="16"
Binding="{Binding Path=Quantity}" />
<DataGridTemplateColumn Header="Items">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding items}">
<DataGrid.Columns>
<DataGridTextColumn Header="Item Name" Width="Auto" FontSize="16"
Binding="{Binding Path=ItemName}" />
<DataGridTextColumn Header="Item Quantity " Width="Auto" FontSize="16"
Binding="{Binding Path=ItemQuantity }" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

Categories