0

일부 FrameworkElements를 동적으로 생성해야합니다. 나는 StackPanel에 있고, 나는 1 PivotItem으로, 그것은 피벗의 내부에 생성하고있는 ScrollViewer 안에 StackPanel에와 버튼이 같은 :ScrollView가 동적으로 생성 된 요소 내에서 작동하지 않습니다.

Pivot pivot = new Pivot(); 
PivotItem pivotItem = new PivotItem(); 

pivot.Items.Add(pivotItem); 

ScrollViewer scrollViewer = new ScrollViewer(); 
StackPanel stackContent = new StackPanel(); 

scrollViewer.Content = stackContent; 

pivotItem.Content = scrollViewer; 

stackContent.Children.Add(new Button() { Content = "button 1" }); 
stackContent.Children.Add(new Button() { Content = "button 2" }); 
stackContent.Children.Add(new Button() { Content = "button 3" }); 
stackContent.Children.Add(new Button() { Content = "button 4" }); 
stackContent.Children.Add(new Button() { Content = "button 5" }); 
stackContent.Children.Add(new Button() { Content = "button 6" }); 
stackContent.Children.Add(new Button() { Content = "button 7" }); 
stackContent.Children.Add(new Button() { Content = "button 8" }); 
stackContent.Children.Add(new Button() { Content = "button 9" }); 
stackContent.Children.Add(new Button() { Content = "button 10" }); 
stackContent.Children.Add(new Button() { Content = "button 11" }); 

stkPanel.Children.Add(pivot); 

을하고이 C#의 XAML 코드 :

<StackPanel x:Name="stkPanel"> 
</StackPanel> 

XAML에서 모든 요소를 ​​만들려고하면 ScrollViewer가 예상대로 작동하지만 페이지에서 일부 이벤트가 발생할 때 동적으로 만들어야합니다.

디버그에서 페이지를 검사하면 PivotItem의 "ActualHeight == 0"이므로 ScrollViewer가 그 이유 때문에 작동하지 않는다고 생각합니다. 그러나 ScrollViewer에 대한 수정 방법을 모르겠습니다. 스크롤을 작동 시키려면 "스스로 충전"하십시오.

+0

XAML에서 모든 것을 만들 때 작업 코드를 추가 할 수 있습니까? 하지만 기본적으로 StackPanel은 필요한만큼 많은 공간을 사용하므로 StackPanel의 ScrollViewer는 내가 틀리지 않으면 작동하지 않아야합니다 ... – schumi1331

+0

이것은 문제입니다, StackPanel ... 내가 Grid로 변경하면 모든 것이 작동합니다. 벌금! – CarlosTI

답변

0

요소에 MaxHeight 을 지정하면 IsVerticalScrollEnabled를 true로 설정해야하지만 더 나은 솔루션은 스크롤바를 표시 할 최대 높이 특성을 제공하는 ListView 또는 GridView입니다.

<StackPanel x:Name="stkPanel" isVerticalScrollEnabled = "true" MaxHeight="300"> 
</StackPanel> 
1

문제는 StackPanel입니다. Schumi1331에서 말했듯이, 스택 패널은 필요한만큼의 공간을 사용합니다. 아들은 폭이나 높이를 정의하지 않으며 scrollViewer는 예상대로 작동하지 않습니다. 그리드로 변경하면 잘 작동합니다.

다른 해결책은 dinnamics 요소를 삽입 한 후이 요소를 사용하는 공간을 계산하고 StackPanel에 높이를 설정하는 것입니다.

+0

그리드 대신 StackPanel에 Pivot을 배치하면 어떤 이점이 있습니까? – schumi1331

+0

필자의 경우, 요소는 webService의 응답으로 동적으로 생성되며 피벗, 텍스트 상자 등과 같이 많은 유형이 있습니다. 따라서 컨테이너 (Stackpanel 또는 Grid 또는 다른 컨테이너)가 있어야 요소가 다른 요소 아래 놓일 수 있습니다. 그리드보다 Stackpanel에 놓기 쉽습니다 (각 요소의 행 번호를 설정해야 함). 어쨌든, 나는 격자를 사용하는 함수를 recoded, 잘 작동! – CarlosTI

+0

나는 본다. 어쩌면 RelativePanel과 Below-properties를 사용하는 것이 다음에 선택할 수있는 옵션일까요? – schumi1331

0
Pivot pivot = new Pivot(); 
     PivotItem pivotItem = new PivotItem(); 

     pivot.Items.Add(pivotItem); 

     ScrollViewer scrollViewer = new ScrollViewer(); 
     StackPanel stackContent = new StackPanel(); 

     scrollViewer.Content = stackContent; 
     Window.Current.Activate(); 
     var bounds = ApplicationView.GetForCurrentView().VisibleBounds; 
     scrollViewer.Height = bounds.Height; 
     pivotItem.Content = scrollViewer; 

     stackContent.Children.Add(new Button() { Content = "button 1" }); 
     stackContent.Children.Add(new Button() { Content = "button 2" }); 
     stackContent.Children.Add(new Button() { Content = "button 3" }); 
     stackContent.Children.Add(new Button() { Content = "button 4" }); 
     stackContent.Children.Add(new Button() { Content = "button 5" }); 
     stackContent.Children.Add(new Button() { Content = "button 6" }); 
     stackContent.Children.Add(new Button() { Content = "button 7" }); 
     stackContent.Children.Add(new Button() { Content = "button 8" }); 
     stackContent.Children.Add(new Button() { Content = "button 9" }); 
     stackContent.Children.Add(new Button() { Content = "button 10" }); 
     stackContent.Children.Add(new Button() { Content = "button 11" }); 

     stkPanel.Children.Add(pivot); 
+0

이 솔루션을 테스트 해 보셨습니까? 나는했지만, 작동하지 않는 것처럼 보였다. (내가 previus commets에서 말했듯이, 스택 패널과의 관계가있는 것으로 보인다. – CarlosTI