ปัญหาการผูกใน C # wpf

ฉันมีปัญหาในการผูกข้อมูลเล็กน้อยใน wpf ฉันมีกล่องข้อความที่สามารถป้อนข้อมูลได้ จากนั้นฉันก็พยายามผูกอินพุตข้อความกับตัวควบคุมผู้ใช้แบบกำหนดเอง สิ่งนี้ใช้ได้กับ usercontrol ภายใน RowDetailsTemplate แต่ไม่ใช่ใน CellTemplate สำหรับแต่ละวัตถุใน CellTemplate ฉันได้รับผลลัพธ์ข้อผิดพลาดนี้:

ข้อผิดพลาด System.Windows.Data: 4: ไม่พบแหล่งที่มาสำหรับการผูกกับการอ้างอิง 'ElementName=ScaleTextBox' BindingExpression:Path=ข้อความ; รายการข้อมูล=null; องค์ประกอบเป้าหมายคือ 'แผนภูมิ' (ชื่อ = ''); คุณสมบัติเป้าหมายคือ '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);
}

ฉันทำอะไรผิด?


person Cinaird    schedule 08.06.2010    source แหล่งที่มา
comment
เป็นไปได้ที่ซ้ำกัน: stackoverflow.com/questions/1089650 /   -  person Amsakanna    schedule 08.06.2010


คำตอบ (3)


จาก ลิงค์นี้

คอลเลกชันคอลัมน์เป็นเพียงคุณสมบัติใน Datagrid คอลเลกชันนี้ไม่ได้อยู่ในแผนผังลอจิคัล (หรือภาพ) ดังนั้น DataContext จึงไม่ได้รับการสืบทอด ซึ่งทำให้ไม่มีสิ่งใดที่จะผูกไว้

ดังนั้นมันจึงใช้ได้กับ RowDetailsTemplate ของคุณไม่ใช่สำหรับคอลัมน์ของคุณฉันเดา

person Amsakanna    schedule 08.06.2010
comment
นั่นอธิบายได้ ดูว่าฉันสามารถจัดการแก้ไขปัญหาได้หรือไม่ คงจะดีถ้าค้นหาโซลูชัน XAML ขอบคุณสำหรับข้อมูล. - person Cinaird; 08.06.2010
comment
ปัญหานี้เกี่ยวข้องกับปัญหาของ NameScope ไม่ใช่ DataContext การใช้ ElementName Binding จะกำหนดแหล่งที่มาอย่างชัดเจน แทนที่จะถอยกลับไปยัง DataContext ปัจจุบันเป็นแหล่งที่มา ข้อความเกี่ยวกับแผนผังตรรกะและภาพยังคงใช้ได้ดี - person John Bowen; 08.06.2010

คุณสามารถลองสิ่งนี้:

<DataTemplate>
   <my:UserControl 
     ItemsSource="{Binding Path=Samples}" 
     MaxValue="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:YourControlClassName}}, ElementName=ScaleTextBox, Path=Text}"/>
</DataTemplate> 
person Andy    schedule 08.06.2010
comment
ฉันได้รับ innerException: Binding.ElementName ไม่สามารถตั้งค่าได้ในขณะที่ใช้ Binding.RelativeSource ฉันก็ลองสิ่งนี้เหมือนกัน แต่ปัญหาเดียวกัน: AncestorType={x:Type DockPanel} - person Cinaird; 08.06.2010
comment
คุณสามารถสร้างคุณสมบัติบนการควบคุมของคุณที่เรียกว่า 'ScaleFactor' เพื่อเก็บค่าของตัวประกอบมาตราส่วน ผูกคุณสมบัติข้อความของกล่องข้อความเข้ากับมัน (TwoWay) จากนั้นในการใช้งาน DataTemplate: {Binding RelativeSource={.. .}, เส้นทาง=ปัจจัยมาตราส่วน, ...} - person Andy; 08.06.2010

ตกลง ฉันแก้ไขมันโดยใช้ ElementSpy เพื่อดูว่า elementSpy ทำงานอย่างไร ดูที่นี่: http://joshsmithonwpf.wordpress.com/2008/07/22/enable-elementname-bindings-with-elementspy/

และ xaml:

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