WPF TabIndex doesn't work correctly - c#

I have a Problem with setting up the TabIndexes correctly. I have a Usercontrol with a GroupBox.
<GroupBox KeyboardNavigation.TabNavigation="Local" TabIndex="0">
<Grid KeyboardNavigation.TabNavigation="Continue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0" KeyboardNavigation.TabNavigation="Continue">
<my:uscZeitraum TabIndex="1" />
<Label />
<ComboBox TabIndex="2" />
<Label />
<ComboBox TabIndex="3" HorizontalAlignment="Right" VerticalAlignment="Top" Width="88" />
<Label />
<ComboBox TabIndex="4" HorizontalAlignment="Right" VerticalAlignment="Top" Width="67" />
<Label />
<ComboBox TabIndex="5" IsEditable="True" GotFocus="cboVorgang_GotFocus" HorizontalAlignment="Right" VerticalAlignment="Top" Width="93" />
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1" KeyboardNavigation.TabNavigation="Continue">
<Label />
<ComboBox TabIndex="6" HorizontalAlignment="Left" Width="192" Height="23" VerticalAlignment="Bottom" />
<Label />
<ComboBox TabIndex="7" Height="23" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="225" />
<Label />
<ComboBox IsEditable="True" TabIndex="8" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="67" />
<Label />
<ComboBox IsEditable="True" TabIndex="9" Width="80" />
</StackPanel>
</Grid>
</GroupBox>
The first Control is also a UserControl but this works like I want to. But if I reach the third ComboBox in the first StackPanel it just starts at the beginning again. It doesn't focus the fourth ComboBox and also doesn't jump into the second Row/Stackpanel. What could that be?

There must be some code in cboVorgang_GotFocus which is doing that. The code written there would be changing the tab order.

Related

Binding DataGrid in WPF [duplicate]

I am doing a simple DataBinding application in WPF. In this app whatever I type in a TextBox named txtName, should appear in the Label. For this purpose I used binding in WPF, but its not getting bound.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="315*" />
<ColumnDefinition Width="188*" />
</Grid.ColumnDefinitions>
<TextBox Height="23" HorizontalAlignment="Left"
Margin="79,12,0,0" Name="txtName"
VerticalAlignment="Top" Width="120" />
<Label Height="28" HorizontalAlignment="Left"
Margin="22,12,0,0" Name="label1"
VerticalAlignment="Top" Width="42"
Content="Name"/>
<TextBox Height="23" HorizontalAlignment="Left"
Margin="79,56,0,0" Name="txtSirName"
VerticalAlignment="Top" Width="120" />
<Label Content="SirName" Height="28"
HorizontalAlignment="Left" Margin="12,56,0,0"
Name="label2" VerticalAlignment="Top" Width="52" />
<!-- Following is the label which I bound -->
<Label Content="{Binding ElementName=txtName,Path=text}"
Height="28" HorizontalAlignment="Left" Margin="47,112,0,0"
Name="label3" VerticalAlignment="Top" Width="171" />
</Grid>
I am new to WPF. Please guide me.
Pretty sure XAML is case sensitive. Your binding path is text when it should be Text.
Path=*T*ext
It's a typo problem.
TextBox has property Text, not text. Small mistake :)
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="315*" />
<ColumnDefinition Width="188*" />
</Grid.ColumnDefinitions>
<TextBox Height="23" HorizontalAlignment="Left" Margin="79,12,0,0" Name="txtName" VerticalAlignment="Top" Width="120" />
<Label Height="28" HorizontalAlignment="Left" Margin="22,12,0,0" Name="label1" VerticalAlignment="Top" Width="42" Content="Name"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="79,56,0,0" Name="txtSirName" VerticalAlignment="Top" Width="120" />
<Label Content="SirName" Height="28" HorizontalAlignment="Left" Margin="12,56,0,0" Name="label2" VerticalAlignment="Top" Width="52" />
<Label Content="{Binding ElementName=txtName,Path=Text}" Height="28" HorizontalAlignment="Left" Margin="47,112,0,0" Name="label3" VerticalAlignment="Top" Width="171" />
</Grid>
Try this
<Label Content="{Binding Source={x:Reference txtName}, Path=text}" Height="28" HorizontalAlignment="Left" Margin="47,112,0,0" Name="label3" VerticalAlignment="Top" Width="171" />

Xamarin Forms display data grid

i'm a beginner with xamarin forms and i have to display dynamic data in grid form. My data has multiple columns and rows, which doesn't always fit the screen size. So i must scroll horizontal and vertical. The first column is a description of the data, I called it row header. My goal is to move/scroll the data "under" the columns and "under" the row header. The columns and the row header must always be visible.
Perhaps somebody had the same problem/solution and point me to the right direction? Or a sample?
Additional info:
To be clearer. The column header must scroll left, if the user scrolls the data area left. Because not all columns could display on screen. Same with the row header. It must scroll down, if user scrolls the data area down.
In excel the function is named "Freeze Panes":
My ugly powerpoint:
Thank you
This works for me on Android and iOS. On UWP it doesn't scroll smoothly. But thats not important for me:
<Grid BackgroundColor="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="Empty" Grid.Row="0" Grid.Column="0" />
<AbsoluteLayout Grid.Column="1"
Grid.Row="0"
VerticalOptions="FillAndExpand" >
<ScrollView x:Name="colScrollView"
Orientation="Horizontal"
VerticalScrollBarVisibility="Never"
HorizontalScrollBarVisibility="Never"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All">
<Grid BackgroundColor="LightGoldenrodYellow">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Text="Column" Grid.Row="0" Grid.Column="0" />
<Label Text="Column" Grid.Row="0" Grid.Column="1" />
<Label Text="Column" Grid.Row="0" Grid.Column="2" />
<Label Text="Column" Grid.Row="0" Grid.Column="3" />
<Label Text="Column" Grid.Row="0" Grid.Column="4" />
</Grid>
</ScrollView>
<BoxView AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All"
BackgroundColor="Transparent"></BoxView>
</AbsoluteLayout>
<AbsoluteLayout Grid.Column="0"
Grid.Row="1"
VerticalOptions="FillAndExpand">
<ScrollView x:Name="rowScrollView"
Orientation="Vertical"
VerticalScrollBarVisibility="Never"
HorizontalScrollBarVisibility="Never"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All">
<Grid BackgroundColor="LightBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="100" />
<RowDefinition Height="100" />
<RowDefinition Height="100" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Label Text="Cell" Grid.Column="0" Grid.Row="0" />
<Label Text="Cell" Grid.Column="0" Grid.Row="1" />
<Label Text="Cell" Grid.Column="0" Grid.Row="2" />
<Label Text="Cell" Grid.Column="0" Grid.Row="3" />
<Label Text="Cell" Grid.Column="0" Grid.Row="4" />
</Grid>
</ScrollView>
<BoxView AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All"
BackgroundColor="Transparent"></BoxView>
</AbsoluteLayout>
<AbsoluteLayout Grid.Column="1"
Grid.Row="1"
VerticalOptions="FillAndExpand">
<ScrollView x:Name="dataScrollView"
Orientation="Both"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All">
<Grid BackgroundColor="LightGreen">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="100" />
<RowDefinition Height="100" />
<RowDefinition Height="100" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Label Text="Cell" Grid.Column="0" Grid.Row="0" />
<Label Text="Cell" Grid.Column="0" Grid.Row="1" />
<Label Text="Cell" Grid.Column="0" Grid.Row="2" />
<Label Text="Cell" Grid.Column="0" Grid.Row="3" />
<Label Text="Cell" Grid.Column="0" Grid.Row="4" />
<Label Text="Cell" Grid.Column="1" Grid.Row="0" />
<Label Text="Cell" Grid.Column="1" Grid.Row="1" />
<Label Text="Cell" Grid.Column="1" Grid.Row="2" />
<Label Text="Cell" Grid.Column="1" Grid.Row="3" />
<Label Text="Cell" Grid.Column="1" Grid.Row="4" />
<Label Text="Cell" Grid.Column="2" Grid.Row="0" />
<Label Text="Cell" Grid.Column="2" Grid.Row="1" />
<Label Text="Cell" Grid.Column="2" Grid.Row="2" />
<Label Text="Cell" Grid.Column="2" Grid.Row="3" />
<Label Text="Cell" Grid.Column="2" Grid.Row="4" />
<Label Text="Cell" Grid.Column="3" Grid.Row="0" />
<Label Text="Cell" Grid.Column="3" Grid.Row="1" />
<Label Text="Cell" Grid.Column="3" Grid.Row="2" />
<Label Text="Cell" Grid.Column="3" Grid.Row="3" />
<Label Text="Cell" Grid.Column="3" Grid.Row="4" />
<Label Text="Cell" Grid.Column="4" Grid.Row="0" />
<Label Text="Cell" Grid.Column="4" Grid.Row="1" />
<Label Text="Cell" Grid.Column="4" Grid.Row="2" />
<Label Text="Cell" Grid.Column="4" Grid.Row="3" />
<Label Text="Cell" Grid.Column="4" Grid.Row="4" />
</Grid>
</ScrollView>
</AbsoluteLayout>
</Grid>
Code behind:
public MyPage()
{
InitializeComponent();
dataScrollView.Scrolled += DataScrollView_Scrolled;
}
private async void DataScrollView_Scrolled(object sender, ScrolledEventArgs e)
{
await rowScrollView.ScrollToAsync(0, e.ScrollY, false);
await colScrollView.ScrollToAsync(e.ScrollX, 0, false);
}
You could use Grid as the root layout and ScrollView to show data .
The ScrollView will scroll-able when the data are out of screen .
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<!--Columns-->
<Label Text="title0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="0" Grid.Column="1" />
<Label Text="title1" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="0" Grid.Column="2" />
<Label Text="title2" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="0" Grid.Column="3" />
<Label Text="title3" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="0" Grid.Column="4" />
<!--Row Header-->
<Label Text="title0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="1" Grid.Column="0" />
<Label Text="title1" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="2" Grid.Column="0" />
<Label Text="title2" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="3" Grid.Column="0" />
<Label Text="title3" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="4" Grid.Column="0" />
<Label Text="title4" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="5" Grid.Column="0" />
<Label Text="title5" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="6" Grid.Column="0" />
<Label Text="title6" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="7" Grid.Column="0" />
<Label Text="title7" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="8" Grid.Column="0" />
<!--Data-->
</Grid>
</ScrollView>
if you use xamarin.forms syncfusion datagrid (SfDataGrid) you have two options to solve this:
1)-the first by using the Unbound Rows and Unbound Columns feature of the SfDataGrid.
2)-second by using the Freeze Panes (also called Frozen Views) feature of the SfDataGrid.
if it was to me i would go for the second option which is Freeze Panes , here is how it looks like:
syncfusion has a free community licence that you can use, the community licence keys can be generated once you open a free account in there page.
once you have the key follow the setup instructions on how to setup the SfDataGrid, the SfDataGrid is just a small NuGet package.
then in your App class constractor past the key as a parameter for the RegisterLicense method.
public App()
{
//Register Syncfusion license
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR LICENSE KEY");
InitializeComponent();
MainPage = new App1.MainPage();
}
for more on licensing.

Xaml Grid Wows Text Box not appearing

I am trying to use grid system as suggestion by another user to another question of mine I have tried to implement the grid system but with little success it is placing the buttons and the grid where i want them but the textbox for name and with should be at the bottom of the listview
These three controls should appear at the bottom of the list view
<TextBox Name="txtDsiplayName" Text="{Binding Path=CustomColumnsDisplayName, Mode=TwoWay}" Height="23"
Width="452" Margin="0,149.86,0,188.5" Grid.ColumnSpan="2" />
<Label Content="Width:" Height="400" VerticalAlignment="Bottom" Grid.ColumnSpan="2" HorizontalAlignment="Left" Width="452" />
<TextBox Name="txtWdith" Width ="30" Height="23" Margin="211,149.86,6,188.5" Grid.Row="1" />
What am I doing wrong?
But as you see the text box is not showing correctly and the list view should be aligned to the top of the move up button winforms I forgive thee and still love ya.
<Grid Margin="0,20,0,0" VerticalAlignment="Top" Height="400">
<Grid.RowDefinitions>
<RowDefinition Height="28" />
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Content="Custom Fields" Margin="12,0,0,0" Grid.ColumnSpan="2"></Label>
<StackPanel Grid.Row="1" Grid.Column="0" VerticalAlignment="Top" Grid.ColumnSpan="2">
<Expander Margin="0,0,0,0"
IsExpanded="true"
Header="Custom Columns">
<Grid Margin="12,0,10,0">
<Grid.RowDefinitions>
<RowDefinition Height="38.64" />
<RowDefinition Height="361.36"/>
<RowDefinition Height="*"/>
<RowDefinition Height="400"/>
<RowDefinition Height="400"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="247" />
<ColumnDefinition Width="205"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ListView Name="listView1"
MinHeight="80"
SelectionMode="Single" Margin="0,11.36,0,108.4" SelectionChanged="listView1_SelectionChanged" Grid.Row="1" Grid.ColumnSpan="2">
<ListView.View>
<GridView>
<GridViewColumn Header="Order" Width="100"
DisplayMemberBinding="{Binding Path=CustomColumnsOrder}"></GridViewColumn>
<GridViewColumn Header="Display Name" Width="290"
DisplayMemberBinding="{Binding Path=CustomColumnsDisplayName}"></GridViewColumn>
<GridViewColumn Header="Width" Width="50"
DisplayMemberBinding="{Binding Path=CustomColumnsWidth}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Label Content="Name:" Grid.ColumnSpan="2" />
<TextBox Name="txtDsiplayName" Text="{Binding Path=CustomColumnsDisplayName, Mode=TwoWay}" Height="23"
Width="452" Margin="0,149.86,0,188.5" Grid.ColumnSpan="2" />
<Label Content="Width:" Height="400" VerticalAlignment="Bottom" Grid.ColumnSpan="2" HorizontalAlignment="Left" Width="452" />
<TextBox Name="txtWdith" Width ="30" Height="23" Margin="211,149.86,6,188.5" Grid.Row="1" />
<StackPanel Grid.Column="3" Grid.Row="2" Margin="0,0,74,0" Grid.RowSpan="2"/>
<StackPanel Grid.Column="2" Margin="0" Grid.RowSpan="2" Grid.ColumnSpan="2">
<Button Name="moveUpButton" Click="MoveUp" DockPanel.Dock="Right" Content="Move Up"
Height="22" Width="74" />
<Button Name="moveDownButton" Click="MoveDown" DockPanel.Dock="Right" Content="Move Down"
Height="22" Width="74" />
<Button Name="deleteButton" IsEnabled="{Binding ElementName=columnsList, Path=SelectedItems.Count}" Click="RemoveColumn" DockPanel.Dock="Right" Content="Delete"
Height="22" Width="74" />
<Button Name="addButton" Click="AddColumn" Content="Add Item"
Height="22" Width="74" />
</StackPanel>
</Grid>
</Expander>
</StackPanel>
</Grid>
It looks like the grid row height and the text box margin are conflicting with each other, if you remove the margin line from the text box you should see it moves to the top.
I would suggest removing the margins and just try to make use of heights to position the controls.
Something like this maybe? (You will need to add back in your heights/widths etc, but it gives you a rough idea)
<Grid VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Content="Custom Fields" />
<StackPanel Grid.Row="1" VerticalAlignment="Top">
<Expander Header="Custom Columns" IsExpanded="true">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListView Name="listView1" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=CustomColumnsOrder}" Header="Order" />
<GridViewColumn DisplayMemberBinding="{Binding Path=CustomColumnsDisplayName}" Header="Display Name" />
<GridViewColumn DisplayMemberBinding="{Binding Path=CustomColumnsWidth}" Header="Width" />
</GridView>
</ListView.View>
</ListView>
<StackPanel Grid.Row="1" Grid.RowSpan="2" Grid.Column="3">
<Button Name="moveUpButton" Content="Move Up" DockPanel.Dock="Right" />
<Button Name="moveDownButton" Content="Move Down" DockPanel.Dock="Right" />
<Button Name="deleteButton" Content="Delete" DockPanel.Dock="Right" IsEnabled="{Binding ElementName=columnsList, Path=SelectedItems.Count}" />
<Button Name="addButton" Content="Add Item" />
</StackPanel>
<Label Grid.Row="3"
Grid.Column="0"
Grid.ColumnSpan="2"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Content="Name:" />
<TextBox Name="txtDsiplayName" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding Path=CustomColumnsDisplayName, Mode=TwoWay}" />
<Label Grid.Row="4"
Grid.Column="0"
Grid.ColumnSpan="2"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Content="Width:" />
<TextBox Name="txtWdith" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" />
</Grid>
</Expander>
</StackPanel>
</Grid>

How to access combobox inside control template in WPF?

I have a control template defined like this:
<Window.Resources>
<ControlTemplate x:Key="fiscalItemsControlTemplate">
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="17" />
<RowDefinition Height="19" />
<RowDefinition Height="17" />
<RowDefinition Height="19" />
<RowDefinition Height="17" />
<RowDefinition Height="19" />
</Grid.RowDefinitions>
<Label Padding="0" Grid.Row="0" Content="{DynamicResource AmmountStr}" HorizontalAlignment="Left" Name="lblAmmount" VerticalAlignment="Bottom" Height="17"/>
<TextBox Padding="0" Name="txtAmmount" Grid.Row="1" Height="19" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path=Amount, Converter={StaticResource moneyConverter}}" />
<Label Padding="0" Content="PurchasePrice" Grid.Row="2" Grid.RowSpan="2" Height="17" HorizontalAlignment="Left" Name="lblPurchasePrice" VerticalAlignment="Top" />
<TextBox Padding="0" Grid.Row="3" Grid.RowSpan="2" Height="19" HorizontalAlignment="Left" Name="txtPurchasePrice" VerticalAlignment="Top" Width="189" Text="{Binding Path=PurchasePrice, Converter={StaticResource moneyConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnSourceUpdated=True}" Validation.Error="Validation_Error" PreviewTextInput="NumericOnly" />
<Label Padding="0" Grid.Row="4" Name="lblOrderState" HorizontalAlignment="Left" Content="Order State" Height="17" />
<ComboBox Padding="0" Grid.Row="5" HorizontalAlignment="Left" Name="cbOrderState" Height="19" Width="189" >
</ComboBox>
</Grid>
</ControlTemplate>
</Window.Resources>
What I'm trying to do is to access combobox "cbOrderState" in codebehind and declare it's itemssours there. I know there are some ways with the FindName() method but how to use it when control template is defined in Window.Resources?
You can use a CollectionViewSource:
<Window.Resources>
<CollectionViewSource x:Key="ViewName"/>
</Window.Resources>
and use in your combobox:
<ComboBox Padding="0" Grid.Row="5" HorizontalAlignment="Left" Name="cbOrderState" Height="19" Width="189" ItemsSource="{Binding Source={StaticResource ViewName}}" >
and populate data in codebehind:
CollectionViewSource yourView = ((CollectionViewSource)(this.FindResource("ViewName")));
yourView.Source = yourCollection;
First of all it's not a good practise to access template and set it's properties from code behind when you have binding already to support that.
Now, even in case you want to do that FindName() is a way to go. You need to access Template from the control on which this resource is applied.
Say you have comboBox declared like this:
<ComboBox x:Name="cmb" Template="{StaticResource fiscalItemsControlTemplate}"/>
You can access from code behind like this:
var comboBox = cmb.Template.FindName("cbOrderState", cmb);

Databind to the datacontext element of the row

I've this itemtemplate binded on a ObservableCollection<MembershipUserProfiled> element so it can be displayed in a ListBox.
<UserControl.Resources>
<ResourceDictionary>
<DataTemplate x:Key="ListItemElencoTemplate">
<Grid Background="#FFFFFF" Width="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="110" />
<ColumnDefinition Width="110" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Style="{DynamicResource label_default}" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Content="{Binding Path=FirstName}" Margin="0 0 0 3" />
<Label Grid.Column="1" Style="{DynamicResource label_default}" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Content="{Binding Path=LastName}" Margin="0 0 0 3" />
<Label Grid.Column="2" Style="{DynamicResource label_default}" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Content="{Binding Path=BaseUser.UserName}" Margin="0 0 0 3" />
<Button Content="Button" Style="{DynamicResource edit}" Click="Edit_Click" CommandParameter="{Binding Path=...}" HorizontalAlignment="Right" Grid.Column="4" Width="20" Height="20"/>
</Grid>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
Everything works just fine but I want to bind the button CommandParameter to the MembershipUserProfiled object of the row, in order to pass the data on the click action.
I've tried different solution but nothing seems to work. What I'm trying to do is feasible?
You can try CommandParameter="{Binding}" this will automatically bind the MembershipUserProfiled object
<Button Content="Button" CommandParameter="{Binding}" Style="{DynamicResource edit}" Click="Edit_Click" HorizontalAlignment="Right" Grid.Column="4" Width="20" Height="20"/>

Categories

Resources