I'm using the WPF combobox for filtering the items but I decide to put it in my datagrid instead but i can't get it to work probably in their i can only get it to work when is outside of the datagrid.
I think the problem is because
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, or ElementName=win
isn't supported inside the datagrid so how do I getting it to work.
this is the error I get
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=SelectedParam; DataItem=null; target element is 'ComboBox' (Name='Krydsmålbox'); target property is 'SelectedValue' (type 'Object')
<DataGrid x:Name="hjuldata"
ItemsSource="{Binding Source={StaticResource cvsTasks}}"
CanUserAddRows="False" BorderBrush="#FF303030" Foreground="#FF00FB0B" Background="#FF303030" AutoGenerateColumns="False" GridLinesVisibility="None" VerticalAlignment="Center" Height="644" Canvas.Left="20" Canvas.Top="257" Width="1250" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Krydsmålet}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
<DataGridTextColumn.Header>
<ComboBox x:Name="Krydsmålbox" Foreground="#FFEAEAEA" Background="#FF303030" FontSize="12"
Style="{StaticResource ComboBoxTest2}" ItemTemplate="{StaticResource cmbTemplate2}"
ItemsSource="{Binding}" SelectedValuePath="Krydsmålene"
SelectedValue = "{Binding SelectedParam, RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}},UpdateSourceTrigger=PropertyChanged}" BorderBrush="#FF303030" Height="40" DockPanel.Dock="Top" IsSynchronizedWithCurrentItem="True" SelectionChanged="Krydsmålbox_SelectionChanged" Canvas.Left="813" Canvas.Top="96" Width="146"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
data template
<DataTemplate x:Key="cmbTemplate2">
<WrapPanel Margin="0 5 0 5" Height="30">
<Image Width="10" Height="20" Stretch="Fill" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,15,0"/>
<Label Content="{Binding Krydsmålene}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" Foreground="#FF00FB0B"/>
</WrapPanel>
</DataTemplate>
CS
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string SelectedParam
{
get { return _selectedParam; }
set
{
_selectedParam = value; OnPropertyChanged("SelectedParam");
if (_selectedParam == "Krydsmål") { BindData(); } else { hjuldata.ItemsSource = FilterKategori().Tables[0].DefaultView; ; }
}
}
public DataSet DTbindkryds()
{
Data = @"SELECT Krydsmålene FROM Data.Krydsmål";
//SQL statement to fetch entries from Krydsmål
DataSet dsdata = new DataSet();
//Open SQL Connection
using (conn = new SqlConnection(connStrings))
{
conn.Open();
//Initialize command object
using (conn = new SqlConnection(connStrings))
using (cmd = new SqlCommand(Data, conn))
{
SqlDataAdapter adapters = new SqlDataAdapter(cmd);
//Fill the result set
adapters.Fill(dsdata);
conn.Close();
}
}
return dsdata;
}
private void bindkrydsmål()
{
Krydsmålbox.ItemsSource = DTbindkryds().Tables[0].DefaultView;
}
<DataGridTextColumn.Header> <DataTemplate> <ComboBox </DataTemplate>
then it says system.windows.datatemplate in my combobox and my combobox don't exists in the curren't context – omini dataBinding
, yourPath
should point toDataContext.SelectedParam
. – XAMlMAX