2013-12-23 3 views
0

나는 UserControl을 표시하는 ContentPresenter입니다. UserControl에는 이미지 목록에 바인딩하는 ItemsControl이 포함되어 있습니다. 내 UserControl에 대해 DataContext을 변경하면 이미지가 표시되지만 표시되지 않습니다. 나는 일어나고 있다고 생각합니다. 그들은 포장하는 대신 서로 겹쳐서 쌓아 올리는 것입니다. 그러나 나는 왜 그런지 모르겠습니다. 내가하는 일을 더 잘 이해할 수있는 몇 가지 코드가 있습니다.내 이미지가 내 UserControl에 나타나지 않는 이유는 무엇입니까?

편집 : 여기서 프로그램이 어떻게 진행되는지에 대한 아이디어를 얻으려면 계속 진행되고 있습니다. ListOfScreenShots은 사용자가 이미지를 선택할 수있는 이미지와 버튼을 표시하는 또 다른 사용자 컨트롤입니다. 스크린 샷을 선택하면 List<BitmapSource>에 추가되며 EnlargedScreenShots에 표시됩니다. 여기에 UserControl 여기

private void MainWindowCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { if (e.Command == SelectImageCommand) { selectedImages.Add(e.Parameter as BitmapSource); ess.DataContext = selectedImages; this._contentPresenter2.Content = ess; } }

을 적용 ListOfScreenShots에 대한 XAML입니다 방법이다. 그것은 또한 무슨 일이 일어나고 있는지 명확하게 이해해야합니다.

ListOfScreenShots의 XAML :

<UserControl x:Class="Client.App.Support.ListOfScreenShots" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      xmlns:support="clr-namespace:Client.App.Support" 
      d:DesignHeight="400" d:DesignWidth="800"> 
    <Grid> 
     <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"> 
      <ItemsControl Name="_listBox" ItemsSource="{Binding ''}"> 
       <!--<ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <WrapPanel Orientation="Vertical"/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel>--> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <Image Name ="_thumbnailImage" Width="600" VerticalAlignment="Center" Source="{Binding ''}"/> 
          <Button VerticalAlignment="Center" HorizontalAlignment="Center" Name="_addscreenshot" Content="Select Screenshot" Command="{x:Static support:SelectScreenShots.SelectImageCommand}" 
            CommandParameter="{Binding ''}" Width="150" /> 
         </StackPanel> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </ScrollViewer> 
    </Grid> 
</UserControl> 

메인 창 XAML : 뒤에

<dxc:DXWindow x:Class="Client.App.Support.SelectScreenShots" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core" Focusable="False" IsTabStop="False" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
    xmlns:libRes="clr-namespace:Shared.Lib.Resources;assembly=Shared.Lib" 
    xmlns:support="clr-namespace:Client.App.Support" 
    Title="Select Images" Height="600" Width="800"> 

<Window.CommandBindings> 
    <CommandBinding Command="support:SelectScreenShots.SelectImageCommand" Executed="MainWindowCommandBinding_Executed"/> 
</Window.CommandBindings> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="367"/> 
      <RowDefinition Height="167"/> 
      <RowDefinition Height="33"/> 
     </Grid.RowDefinitions> 
     <ContentPresenter Grid.Row="0" Name="_contentPresenter" Content="{Binding ''}"/> 
     <ContentPresenter Grid.Row="1" Name="_contentPresenter2" Content="{Binding ''}"/> 
     <StackPanel Grid.Row="2" HorizontalAlignment="Right" Orientation="Horizontal"> 
      <Button Name="_OK_Button" Content="OK" Click="_OK_Button_Click" Margin="0,5,5,5" Width="75" Height="23"/> 
      <Button Name="_Cancel_Button" Content="Cancel" Click="_Cancel_Button_Click" Margin="0,5,5,5" Width="75" Height="23"/> 
     </StackPanel> 
    </Grid> 

주 창 코드 :

public partial class SelectScreenShots : DXWindow 
{ 
    public static readonly RoutedCommand SelectImageCommand = new RoutedCommand(); 

    public static List<BitmapSource> selectedImages = new List<BitmapSource>(); 
    public static List<BitmapSource> screenshots = new List<BitmapSource>(); 
    public static ListOfScreenShots loss = new ListOfScreenShots(); 
    public static EnlargedScreenShot ess = new EnlargedScreenShot(); 

    public SelectScreenShots() 
    { 
     Client.GUI.AppGUI.SetupTheme(this); 
     InitializeComponent(); 
     screenshots = RenderWindows(); 
     loss.DataContext = screenshots; 
     this._contentPresenter.Content = loss; 
    } 

    public static List<BitmapSource> RenderWindows() 
    { 
     var windows = Application.Current.Windows 
             .OfType<Window>() 
             .Where(x => x.GetType() != typeof(AskAQuestionDialog) & x.GetType() != typeof(SelectScreenShots)); 

     var bitmaps = new List<BitmapSource>(); 

     foreach (var window in windows) 
     { 
      var bitmap = new RenderTargetBitmap((int)window.Width, (int)window.Height, 96d, 96d, PixelFormats.Default); 
      bitmap.Render(window); 

      bitmaps.Add(bitmap); 
     } 

     return bitmaps; 
    } 


    private void MainWindowCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) 
    { 
     if (e.Command == SelectImageCommand) 
     { 
      selectedImages.Add(e.Parameter as BitmapSource); 
      ess.DataContext = selectedImages; 
      this._contentPresenter2.Content = ess; 
     } 
    } 


    private void _OK_Button_Click(object sender, RoutedEventArgs e) 
    { 
     this.Close(); 
    } 

    private void _Cancel_Button_Click(object sender, RoutedEventArgs e) 
    { 
     this.Close(); 
    } 
} 
} 
,363,210

사용자 컨트롤의 XAML :

<UserControl x:Class="Client.App.Support.EnlargedScreenShot" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="162" d:DesignWidth="800"> 
<Grid> 
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled"> 
     <StackPanel Orientation="Horizontal"> 
      <ItemsControl Name="_itemsControl" ItemsSource="{Binding ''}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <WrapPanel Orientation="Horizontal"/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
         <Image Name ="_thumbnailImage" HorizontalAlignment="Left" VerticalAlignment="Center" Source="{Binding ''}"/> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
     </StackPanel> 
    </ScrollViewer> 
</Grid> 

가 크기 조정 및 스크롤 막대 가시성이 제대로 작동하지 않을뿐만까지 계속 다른 일을하지만, 나중에 그 밖으로 작동합니다. 지금 이것은 정말로 나를 괴롭 히고있다.

누구나 분명히 잘못된 것을 볼 수 있습니다. 나는 스택 패널에 모든 것이 싸여 있다는 사실과 관련이 있다고 생각하지만, DataTemplate 안에있는 이미지가 StackPanel으로 감싸지기 전에는 여전히 작동하지 않습니다. 또한 StackPanel을 모두 제거하고 WrapPanel을 사용해 보았습니다.

도움을 주시면 감사하겠습니다. 고맙습니다.

+0

그것은 여기에 무슨 일이 일어나고 있는지에 따라 쉽지 않다 : 난 그냥 여기에 속으로

EnlargedScreenShot ess = new EnlargedScreenShot();

를 이동했다. 'ListOfScreenShots' 무엇입니까? 'UserControl'은 어디에 적용됩니까? – McGarnagle

+0

MainWindowCommandBinding_Executed 안에 e.Parameter에 이미지가 있는지 확인하십시오. – jure

+0

@jure 예 명령 매개 변수가 있습니다. 매개 변수의 출처 인'ListOfScreenshots'에서 xaml을 게시했습니다. 이미지가'UserControl'에 추가되고 있는데, 제대로 표시되지 않습니다. – Adam

답변

0

내 코드에서 뭔가 분명히 잘못하고있었습니다.

내 명령이 실행될 때 EnlargedScreenShot의 새 인스턴스를 인스턴스화하지 않았습니다. 창을로드 한 다음 인스턴스를 업데이트하지 않을 때 인스턴스화 중입니다.

private void MainWindowCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) 
{ 
    EnlargedScreenShot ess = new EnlargedScreenShot(); 
    if (e.Command == SelectImageCommand) 
    { 
     selectedImages.Add(e.Parameter as BitmapSource); 
     ess.DataContext = selectedImages; 
     this._contentPresenter2.Content = ess; 
    } 
} 
0

확실한 답을 얻기에는 너무 많은 일들이 있지만 너무 빨리 떠오르는 것은 속성 변경 알림을하지 않는 것입니다. 목록을 ObservableCollection으로 전환하고 this MSDN article on the subject을 읽으십시오.

+0

마크에 대해 죄송합니다. 내 코드가 꽤 추한 걸 알아. 어쩌면 진짜 해결책은 그것을 먼저 정리하고 그렇게함으로써 해결책을 찾을 것입니다. 이 질문을 삭제해야합니까? – Adam

0

내 추측 :

1) UserControl을 0으로 크기가 조정됩니다 0 그래서 당신은 아무것도 볼 수 없습니다. 바인딩의 UserControl 너비 ​​/ ContentPresenter에 너비/높이까지의 높이, 그래서는 :

<UserControl x:Class="Client.App.Support.EnlargedScreenShot" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
     Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
     d:DesignHeight="162" d:DesignWidth="800"> 

2), 이미지 요소에 너비/높이 속성을 추가 테스트를 위해 명시 적으로 설정합니다.또한 TextBlock을 추가하고 Image.Source에 대해 Text를 바인딩하여 바인딩이 제대로 작동하는지 확인하십시오.

이 문제는 몇 분 만에이 질문에 대답합니다 스눕 (WPF 스파이 유틸리티)의 도움이 필요합니다, 그래서 아마도이 지금은 가장 유용한 제안입니다 : http://snoopwpf.codeplex.com/

시 두를 사이에 같은 BitmapSource는을 공유하는이 통제 수단. 나는 정확하게 알지 못한다. 그러나 이것은 물고기 같은 부분일지도 모른다. 그렇게 할 수 있다면 복제 해보십시오.

+0

또한 WPF 비주얼 라이저가 있습니다 ... 버튼 클릭 핸들러 등으로 호출하는 코드 숨김에 중단 점을 추가하고 감시 윈도우에 컨트롤을 추가하고 작은 돋보기를 클릭하십시오. 그런 다음 시각적 트리를 탐색하여 정확히 어떤 부분이 렌더링되는지 확인할 수 있습니다. –

+0

@ Erti-Chris Eelmaa는 제안에 감사드립니다. 그들 중 누구도 일하지 않았습니다. 바인딩이 올바르게 작동하고 사용자 컨트롤간에 BitmapSource 전달시 문제가없는 것 같습니다. BitmapSources가 올바르게 전달되고 ItemSource로 설정되면 내 xaml에 문제가 있다고 생각합니다. 스눕이 드러내는 것을 볼 것입니다. – Adam

관련 문제