数据绑定在资源字典中不起作用
我试图将列表框的 SelectedItem 属性绑定到两个文本框控件。这些控件位于 Windows 资源部分。这个想法是,当选择列表框中的条目时,两个文本框控件显示我的自定义类中的“blockName”和“blockHelpText”。我首先通过单击 but1 按钮加载列表框。
<Window.Resources>
<StackPanel x:Key="testsp" Visibility="Visible" DataContext="{Binding ElementName=lsbCommonBlocks, Path=SelectedItem, diagnostics:PresentationTraceSources.TraceLevel=High}">
<ListBox x:Name="lsbCommonBlocks" DisplayMemberPath="BlockName" SelectionChanged="lb_sc"/>
<Button x:Name="but1" Click="but1_click" Content="Button 1"/>
<TextBox x:Name="txt1" Text="{Binding Path=BlockName, diagnostics:PresentationTraceSources.TraceLevel=High}"/>
<TextBox x:Name="txt2" Text="{Binding Path=BlockHelpText, diagnostics:PresentationTraceSources.TraceLevel=High}"/>
</StackPanel>
</Window.Resources>
<Grid>
<ContentControl Visibility="Visible" x:Name="contentWorkArea" Content="{StaticResource testsp}"/>
</Grid>
我有一个按钮,其单击事件将列表框的 Itemsources 属性与我的自定义类 BlockToolBar 联系起来。
BlockToolBar[] blocks = { new BlockToolBar("Block 1", "No help for this block."),
new BlockToolBar("Block 2", "Help."),
new BlockToolBar("Block 3", "Help again.") };
private void but1_click(object sender, RoutedEventArgs e)
{
StackPanel sp = (StackPanel)this.TryFindResource("testsp");
ListBox lb = (ListBox)LogicalTreeHelper.FindLogicalNode(sp, "lsbCommonBlocks");
lb.ItemsSource = blocks;
}
public class BlockToolBar : INotifyPropertyChanged
{
private string blockName;
public string BlockName
{
get { return blockName; }
set {
blockName = value;
OnPropertyChanged(new PropertyChangedEventArgs("BlockName"));
}
}
private string blockHelpText;
public string BlockHelpText
{
get { return blockHelpText; }
set {
blockHelpText = value;
OnPropertyChanged(new PropertyChangedEventArgs("BlockHelpText"));
}
}
public BlockToolBar()
{
blockName = "";
blockHelpText = "";
}
public BlockToolBar(string BlockName, string BlockHelpText)
{
blockName = BlockName;
blockHelpText = BlockHelpText;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
}
数据绑定不起作用:当我单击按钮时,TextBox 的 txt1 和 txt2 保持空白。列表框填充正常。我确认可以通过在列表框中创建事件来获取 BlockToolBar 属性。
Visual Studio(express)输出窗口显示以下内容: System.Windows.Data 错误:4:找不到与引用“ElementName=lsbCommonBlocks”绑定的源。 BindingExpression:Path=SelectedItem;数据项=空;目标元素是“StackPanel”(名称=“”);目标属性是“DataContext”(类型“Object”)
不确定我做错了什么。我对 WPF 还很陌生。
如果我删除 stackpanel 资源 (testsp) 并将 contentcontrol 替换为 testsp 控件,则数据绑定可以工作。我还可以将 txt1 和 txt2 直接绑定到资源中的 BlockToolBar 对象,但我似乎无法将 txt1/txt2 绑定到资源中的选定项目。
我这样做的原因是我有一个复杂的主窗口,我将其分解为单独的资源字典并通过内容控件调用我的主窗口。当我无法让它工作时,我创建一个更简单的项目来尝试缩小我的问题范围。
I am trying to bind the SelectedItem property of a listbox to two textbox controls. These controls are located in the windows resource section. The idea is when an entry in the listbox gets selected the two textbox controls display the "blockName" and "blockHelpText" from my custom class. I load the listbox first by clicking the but1 button.
<Window.Resources>
<StackPanel x:Key="testsp" Visibility="Visible" DataContext="{Binding ElementName=lsbCommonBlocks, Path=SelectedItem, diagnostics:PresentationTraceSources.TraceLevel=High}">
<ListBox x:Name="lsbCommonBlocks" DisplayMemberPath="BlockName" SelectionChanged="lb_sc"/>
<Button x:Name="but1" Click="but1_click" Content="Button 1"/>
<TextBox x:Name="txt1" Text="{Binding Path=BlockName, diagnostics:PresentationTraceSources.TraceLevel=High}"/>
<TextBox x:Name="txt2" Text="{Binding Path=BlockHelpText, diagnostics:PresentationTraceSources.TraceLevel=High}"/>
</StackPanel>
</Window.Resources>
<Grid>
<ContentControl Visibility="Visible" x:Name="contentWorkArea" Content="{StaticResource testsp}"/>
</Grid>
I have a button who's click event ties the Itemsources property of the listbox to my custom class BlockToolBar.
BlockToolBar[] blocks = { new BlockToolBar("Block 1", "No help for this block."),
new BlockToolBar("Block 2", "Help."),
new BlockToolBar("Block 3", "Help again.") };
private void but1_click(object sender, RoutedEventArgs e)
{
StackPanel sp = (StackPanel)this.TryFindResource("testsp");
ListBox lb = (ListBox)LogicalTreeHelper.FindLogicalNode(sp, "lsbCommonBlocks");
lb.ItemsSource = blocks;
}
public class BlockToolBar : INotifyPropertyChanged
{
private string blockName;
public string BlockName
{
get { return blockName; }
set {
blockName = value;
OnPropertyChanged(new PropertyChangedEventArgs("BlockName"));
}
}
private string blockHelpText;
public string BlockHelpText
{
get { return blockHelpText; }
set {
blockHelpText = value;
OnPropertyChanged(new PropertyChangedEventArgs("BlockHelpText"));
}
}
public BlockToolBar()
{
blockName = "";
blockHelpText = "";
}
public BlockToolBar(string BlockName, string BlockHelpText)
{
blockName = BlockName;
blockHelpText = BlockHelpText;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
}
Databinding does not work: TextBox's txt1 and txt2 remain blank when I click the button. The listbox populates ok. I confirmed that I'm able to obtain the BlockToolBar properties OK by creating an event on the listbox.
Visual Studio (express) output window gives me following: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=lsbCommonBlocks'. BindingExpression:Path=SelectedItem; DataItem=null; target element is 'StackPanel' (Name=''); target property is 'DataContext' (type 'Object')
Not sure what I'm doing wrong. I'm kind new to WPF.
If I remove the stackpanel resource (testsp) and replace the contentcontrol with the testsp controls, then the databindings work. I can also bind txt1 and txt2 directly to the BlockToolBar object in the resource but I can't seem to bind txt1/txt2 to the selecteditem if in the resource.
The reason I'm doing this is I have a complicated main window which I break up into seperate resourcedictionaries and call into my main window via contentcontrols. When I couldn't get that to work I create a simpler project to try to narrow down my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来必须在代码中创建绑定。我做了以下更改并且有效。我以为我以前尝试过这个,但是......
Looks like have to create binding in code. I made following changes and it worked. Thought I tried this before but...