2017-03-11 4 views
1

내가 가진이 DataGrid :로드 데이터 그리드의 행

<DataGrid x:Name="ButtonsDataGrid" Grid.Row="1" Margin="5,5,0,0" 
      ScrollViewer.VerticalScrollBarVisibility="Auto" 
      ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
      Background="Transparent" HeadersVisibility="None" 
      BorderThickness="0" BorderBrush="Transparent"> 
    <DataGridTemplateColumn Header="user_buttons"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <userButtons:GuestButtons/> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid> 

내가 그 DataTemplate의 내부에 넣을 필요가 XAML UserControl :

<UserControl x:Class="HP_hf_shop.BrowseButtons.GuestButtons" 
     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" 
     xmlns:local="clr-namespace:HP_hf_shop.BrowseButtons" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="50"/> 
    </Grid.RowDefinitions> 
    <Button Template="{DynamicResource TileTemplate}"> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="5*"/> 
      </Grid.ColumnDefinitions> 
      <Path Data="M1.5,38.001 L10.666,38.001 10.666,38.005341 C10.666,38.011758 10.666,38.015 10.666,38.015 L57.666903,38.001107 67.909877,38.001 67.909877,81.667664 1.5,81.667664 z M34.188718,1.5000001 C46.680964,1.4988451 57.432162,7.8204405 57.710986,14.7386 57.917159,19.854203 57.674712,37.439694 57.667087,37.987992 L57.666903,38.001 10.666,38.001 10.666,37.976643 C10.665999,37.189652 10.665995,24.468745 10.666321,16.211438 10.666653,7.8482163 20.950168,1.501225 34.188718,1.5000001 z" 
       Style="{DynamicResource MimicVectorStyle}" Stretch="Fill" StrokeThickness="3" d:LayoutOverrides="LeftMargin, RightMargin, TopMargin, BottomMargin" 
        Margin="5,2,5,2" Grid.Column="0"/> 
      <Label Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" 
        HorizontalContentAlignment="Left" VerticalContentAlignment="Center" 
        Content="REGISTER" FontSize="30" FontFamily="CenturyGothicRegual" 
        Margin="0,0,0,0" Style="{DynamicResource MimicLabelStyle}"> 
      </Label> 
     </Grid> 
    </Button> 
</Grid> 

이 문제는 내안에 button이 있어도 DataGrid 행에 아무것도 표시되지 않습니다. 10은 디자이너 내부에서 볼 수 있습니다. 나중에 이것은 역동적이어야합니다. 손님, 관리자 및 고객을위한 버튼 목록이 있으므로 언급 한 행의 버튼을 누르는 것만이 아닙니다. 다시 한 번, XAML UserControl의 내용을 내 DataGrid 행에 표시하려면 어떻게해야합니까?

은 내가 DataGrid 내부

HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" 
VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" 

을 추가했지만 문제가 지속.

코드가 컴파일되고 오류없이 실행됩니다.

전혀 새로운 솔루션을 만들고 this MSDN article에서 XAML 코드를 복사하여 붙여 넣었습니다. 아무 것도 변경되지 않았습니다. 프로젝트를 실행해도 결과가 나오지 않습니다. Visual Studio 2015에서 다시 설치해야합니까 ?? : D

답변

1

나는 샘플을 복사하고 그것이 내가이 문제를 해결 작동하도록 :

1) 데이터 그리드가 ItemsSource가 설정되어하지 않습니다. 해결 방법 :

XAML :

<DataGrid x:Name="ButtonsDataGrid" 
      ItemsSource="{Binding Path=???-some-collection-from-DataContext}" ... 

코드 :

ButtonsDataGrid.ItemsSource = someCollection; 

2) ItemsSource와 내가 XamlParseException있어 설정합니다. DataGridTemplateColumn은 DataGrid.Columns에 추가해야하며 DataGrid에는 직접 추가해야합니다.

<DataGrid.Columns> 
    <DataGridTemplateColumn Header="user_buttons"> 
     <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <wpfDemos:GuestButtons/> 
       </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid.Columns> 
+0

답장을 보내 주셔서 감사합니다. 제 경우에는 작동하도록 노력했지만 불행히도 그럴 수 없었습니다. 나는'DataContext'가 ** UserControl뿐만 아니라 **에서도 호출되었지만 ** 운이 좋지 않은 (디버그 모드에서 체크 된) **에 대해 적절하게 설정되었는지 확인했습니다. 뒤에 코드가 괜찮 으면, 내'GuestButtons' usercontrol 사용을 피해야합니까? – agiro

+0

TemplateColumn의 usercontrol은 문제가 없습니다. 나는 다른 곳에서 문제가 있어야한다고 생각합니다. (내 최선의 추측은 ItemsSource에 대한 잘못된 바인딩입니다). DataGrid는'ButtonsDataGrid.ItemsSource = new int [] {1,2,3};와 함께 어떤 것을 보여 주나요? 그게 내가 질문에서 코드를 시도했을 때 한 것입니다. – ASh

+0

그래, 그것은 작동하고 내가 원하는 주어진 버튼 3 개를 보여줍니다. 비트 오프 형식화하지만 그것을 바로 잡을 것입니다. 자, 어떻게 정수를 내부에 넣었지만 3 개의 버튼이 있습니까? 아니면 잘 될까요? – agiro