我有一个问题,在wpf中的whit绑定我有一个文本框,我可以做一些输入,然后我尝试将textinput绑定到自定义usercontrol . 这适用于RowDetailsTemplate中的usercontrol,但不适用于CellTemplate . 对于CellTemplate中的每个对象,我得到此错误输出:
System.Windows.Data错误:4:无法找到引用'ElementName = ScaleTextBox'的绑定源 . BindingExpression:路径=文本;的DataItem = NULL; target元素是'Chart'(Name =''); target属性是'MaxValue'(类型'Int32')
我的代码看起来像这样:
XAML
<ToolBarTray ToolBarTray.IsLocked="True" DockPanel.Dock="Top" Height="25">
<ToolBar Name="ButtonBar" >
<TextBox Height="23" Name="ScaleTextBox" Width="120" Text="400"/>
</ToolBar>
</ToolBarTray>
<DataGrid ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False" IsReadOnly="True" RowHeight="25" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<my:UserControl ItemsSource="{Binding Path=Samples}" MaxValue="{Binding ElementName=ScaleTextBox, Path=Text}"/>-->
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<DataGridTemplateColumn MinWidth="150" Header="Chart" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<my:UserControl ItemsSource="{Binding Path=Samples}" MaxValue="{Binding ElementName=ScaleTextBox, Path=Text}"/><!-- this is the problem -->
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
C#
public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(int), typeof(Chart), new FrameworkPropertyMetadata(MaxValuePropertyChanged));
private static void MaxValuePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
Console.WriteLine(e.NewValue);
}
我做错了什么?
3 回答
来自this link
因此,它适用于您的RowDetailsTemplate,而不适用于您的列 .
你可以试试这个:
好的,我使用ElementSpy解决了它,看看elementSpy的工作原理如下:http://joshsmithonwpf.wordpress.com/2008/07/22/enable-elementname-bindings-with-elementspy/
和xaml: