2013-07-23 2 views
0

동적으로 C# 코드에서 이미지와 MediaElement를 스택 패널에 추가하려고하지만 다른 요소를 볼 수 없으므로 xaml 코드에서이 문제가 발생합니다. : 내 C# 코드에서스택 패널에 다른 유형의 컨트롤을 동적으로 추가하는 방법 C# WP8

<ListBox x:Name="lstbxUIElements" Width="auto" Height="auto"> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Name="stackContainer" DataContext="{Binding}" Orientation="Horizontal"/> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
</ListBox> 

는 사용자가 사진이나 비디오를 취할 수 있습니다, 그리고 수평, 난 그냥 하나 개의 요소를 볼 수 StackPanel의에서 그들을 보여주고 싶은. 내 C# 코드 :

//Constructor 
public MainPage() 
{ 
    lstbxUIElements.ItemsSource = multiMediaElements;//(List<Objects> multiMediaElements) 
} 

public void MethodVideo() 
{ 
    MediaElement a ="some code to retrieve mp4" 
    multiMediaElements.add(a); 
} 

public void MethodImage() 
{ 
    BitmapImage bmp = new BitmapImage(); 
    bmp.SetSource(e.ChosenPhoto); 
    multiMediaElements.add(bmp); 
} 

제안 사항? (WP8로 작업)

답변

2

올바른 작업에 올바른 컨트롤을 사용하십시오. 당신의 컨트롤을 추가

<StackPanel Name="UIElements" Orientation="Horizontal"/> 

그런 다음 어린이에게 속성을 사용 : 당신이 당신의 패널에 컨트롤을 추가 할 경우 대신 직접 StackPanel의를 사용하여 목록 상자가 필요하지 않습니다

public void MethodImage() 
{ 
    BitmapImage bmp = new BitmapImage(); 
    bmp.SetSource(e.ChosenPhoto); 
    UIElements.Children.Add(bmp); 
} 
+0

예. 시도했지만 더 많은 요소를 추가 할 때 스택 패널의 가로 스크롤 막대가 나타나지 않습니다. 나는 왜 그런지 알지 못합니다. 왜냐하면 수직적 인 형태로 아무런 문제없이 나타나기 때문입니다. –

+1

@ PaulD.LuffyVongolaO'Conner Stackpanel을 스크롤 뷰어에 넣고 HorizontalScrollBarVisibility를 Auto로 설정하십시오. –

0

당신이 만약 목록 상자에 컨트롤을 추가 한 다음 직접 추가하면 항목 템플릿을 정의한 다음 일반 바인딩을 수행 할 필요가 없습니다. 아래 코드와 같이 할 수있다 :

XAML : C# 여러 항목의

BitmapImage bmp = new BitmapImage(); 
bmp.SetSource(e.ChosenPhoto); 
lstbxUIElements.Items.Add(bmp) 

위의 그림과 같이 목록 상자 항목의 요소를 추가 계속

<ListBox x:Name="lstbxUIElements" Width="auto" Height="auto" /> 

관련 문제