简体   繁体   English

C#WPF中的绑定问题

[英]Binding problem in C# wpf

I have a problem whit binding in wpf i have a textbox where i can do some input, then i try to bind the textinput to a custom usercontrol. 我在wpf中绑定绑定时遇到问题,我在其中可以执行一些输入的文本框,然后尝试将textinput绑定到自定义usercontrol。 This work for the usercontrol within RowDetailsTemplate but not in the CellTemplate. 这适用于RowDetailsTemplate中的usercontrol,但不适用于CellTemplate中的usercontrol。 For each object in the CellTemplate i get this error output: 对于CellTemplate中的每个对象,我得到以下错误输出:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=ScaleTextBox'. System.Windows.Data错误:4:找不到参考'ElementName = ScaleTextBox'的绑定源。 BindingExpression:Path=Text; BindingExpression:Path = Text; DataItem=null; DataItem = null; target element is 'Chart' (Name=''); 目标元素是'Chart'(Name =''); target property is 'MaxValue' (type 'Int32') 目标属性是“ MaxValue”(类型“ Int32”)

My code looks like this: 我的代码如下所示:

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);
}

What do i do wrong? 我做错了什么?

From this link 这个连结

The Columns collection is just a property in the Datagrid; Columns集合只是Datagrid中的一个属性。 this collection is not in the logical (or visual) tree, therefore the DataContext is not being inherited, which leads to there being nothing to bind to. 此集合不在逻辑(或视觉)树中,因此不会继承DataContext,这导致没有任何要绑定的对象。

Hence it works for your RowDetailsTemplate and not for your columns I guess. 因此,它适用于您的RowDetailsTemplate而不适用于您的列。

You can try this: 您可以尝试以下方法:

<DataTemplate>
   <my:UserControl 
     ItemsSource="{Binding Path=Samples}" 
     MaxValue="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:YourControlClassName}}, ElementName=ScaleTextBox, Path=Text}"/>
</DataTemplate> 

Ok, i Solved it using ElementSpy, to see how elementSpy works look here: http://joshsmithonwpf.wordpress.com/2008/07/22/enable-elementname-bindings-with-elementspy/ 好的,我使用ElementSpy解决了它,以查看elementSpy的工作原理,请看这里: http : //joshsmithonwpf.wordpress.com/2008/07/22/enable-elementname-bindings-with-elementspy/

and the xaml: 和xaml:

<my:UserControl  local:ElementSpy.NameScopeSource="{StaticResource ElementSpy}" ItemsSource="{Binding Path=Samples}" MaxValue="{Binding ElementName=ScaleTextBox, Path=Text}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM