当我把我的组合框放在我的listview中时,我发现一个奇怪的问题,它说它找不到listview
之外我的元素名称win(我的窗口名称)的引用它工作正常并且没有任何问题接着就,随即。
System.Windows.Data错误:4:找不到引用'ElementName = win'的绑定源。 BindingExpression:路径= SelectedParam;的DataItem = NULL; target元素是'ComboBox'(Name ='Krydsmålbox'); target属性是'SelectedValue'(类型'Object')
<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, ElementName=win, UpdateSourceTrigger=PropertyChanged}" Width="138" BorderBrush="#FF303030" Height="40"/>
CS
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _selectedParam;
public MainWindow()
{
InitializeComponent();
BindData();
ICollectionView dataView = CollectionViewSource.GetDefaultView(hjuldata.ItemsSource);
dataView.GroupDescriptions.Add(new PropertyGroupDescription("Mærke"));
}
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string SelectedParam { get { return _selectedParam; } set { _selectedParam = value; NotifyPropertyChanged("SelectedParam");
if (_selectedParam == "Krydsmål") { BindData(); } else { hjuldata.ItemsSource = FilterKategori().Tables[0].DefaultView; ; } } }
private void BindData()
{
hjuldata.ItemsSource = Kategori().Tables[0].DefaultView;
}
public DataSet FilterKategori()
{
Data = @"Select ps.Mærket AS Mærke, P.DataID, P.Billed, P.Model, P.Årgang, P.[Motor Type], P.Krydsmålet, P.Centerhul, P.ET,P.Bolter, P.Dæk, P.Fælge ,PS.Krydsmålene from Data.Hjuldata P inner join Data.Mærke PS on P.MærkeID = PS.MærkeID WHERE Krydsmålet = @param1";
//SQL statement to fetch entries from products
DataSet dsdata = new DataSet();
//Open SQL Connection
using (conn = new SqlConnection(connStrings))
{
conn.Open();
//Initialize command object
using (cmd = new SqlCommand(Data, conn))
{
cmd.Parameters.AddWithValue("@param1", SelectedParam);
SqlDataAdapter adapters = new SqlDataAdapter(cmd);
//Fill the result set
adapters.Fill(dsdata);
conn.Close();
}
}
return dsdata;
}
答案 0 :(得分:0)
我认为使用RelativeSource更好,而不是按元素名称绑定,如下所示:
<ComboBox x:Name="Krydsmålbox" Foreground="#FFEAEAEA" Background="#FF303030" FontSize="12"
Style="{StaticResource ComboBoxTest2}" ItemTemplate="{StaticResource cmbTemplate2}"
ItemsSource="{Binding}" SelectedValuePath="Krydsmålene"
SelectedValue = "{Binding DataContext.SelectedParam, RelativeSource={RelativeSource FindAncestor,
AncestorType= Window},UpdateSourceTrigger=PropertyChanged}"
Width="138" BorderBrush="#FF303030" Height="40"/>
答案 1 :(得分:0)
当你在ListView中放入一些东西时,它就会进入一个封装的岛屿&#39;它无法到达列表视图之外并直接绑定到视图中的其他项目。您需要使用FindAncestor
绑定
{Binding Path=SelectedParam, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window},
UpdateSourceTrigger=PropertyChanged}