2016-09-25 2 views
-1

에 클래스의 레이아웃 어린이의를 참조하십시오 :어떻게 그런 그에게 줄 내가 그리드를 WPF

<Grid Name="sGrid" Height="650" Width="205" Margin="0,25"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="35"/> 
     </Grid.ColumnDefinitions> 
     <TextBlock Name="minSpeed" HorizontalAlignment="Left" TextWrapping="Wrap" Text="min speed" VerticalAlignment="Bottom" Grid.Column="0" Grid.Row="0"/> 

을 나는이 같은 내 수업이 그리드의 사본을 보내 :

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     sClass sObject = new sClass(sGrid); 
    }}; 

그리고 그런 내 수업 시간에 그것을 얻을 :

public class sClass 
{ 
    public sClass(Grid Xi_Sgrid) 
    { 
     sGrid = Xi_Sgrid; 
     sGraphHeight = sGrid.RowDefinitions[0].ActualHeight; 
     sGraphWidth = sGrid.ColumnDefinitions[0].ActualWidth;; 
     sGrid.minSpeed // can't find in the intellisense 

    } 

지금 sClass의 처음 세 줄은 OK하지만 제 생각하다 불법입니다. 클래스에서 레이아웃 어린이를 참조하는 올바른 방법은 무엇입니까? 덕분에 .

+0

의 사용 가능한 복제 [? WPF는 이름하게 proprty없이 C#에서 같은 부모를 가진 다른 컨트롤을 얻는 방법] (http://stackoverflow.com/questions/39683496/ : 샘플 코드는 아래에 도움이된다면보기 어떻게 다른 사람의 통제를받는 wpf-having-same-parent-in-c-sharp-its-na-na) –

+1

여기에 가능한 답이 너무 많습니다. 그러나 짧은 버전은 "당신은하지 않습니다"입니다. UI 요소를 직접 조작해서는 안됩니다. 대신 뷰 모델에서 프로그램을 기반으로 적절한 속성을 UI 요소 속성에 바인딩하여 사용자에게 표시되도록하십시오. 만약 당신이 그렇게하지 않는다고 주장한다면, 여러분이 보여준 코드를 볼 때'sGrid' 참조와 함께'minSpeed'를'sClass' 생성자에 전달해야한다고 말하고 싶습니다. 당신이 그것을 잘못 할 것이라면 적어도 자신을 힘들게 만드는 대신에 _easy_ way를 잘못하십시오. –

답변

0

Grid에서 FindName 메서드를 사용해야합니다.

TextBlock minSpeed = sGrid.FindName("minSpeed") as TextBlock; 
if (minSpeed != null) 
{ 
    // do something with minSpeed 
} 
+0

@PeterDuniho sGrid 대신 svpGrid를 잘못 입력했습니다. 단일 변수를 가져 주셔서 감사합니다. 코드를 업데이트했습니다. – sachin

관련 문제