2017-02-28 9 views
1

私はカスタムのDataTemplateで、このリストビューを持っている:私は達成しようとしています何変化幅を動的

<ListView Name="lvDataBinding" VerticalAlignment="Top" Margin="0,200,0,30" ScrollViewer.CanContentScroll="False" BorderBrush="Transparent" BorderThickness="0"> 
    <ListView.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal"></StackPanel> 
     </ItemsPanelTemplate> 
    </ListView.ItemsPanel> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <templates:ZoneTemplate Name="dataTemplate" Margin="-5,0,-5,0" Width="160"/> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="ListViewItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
     </Style> 
    </ListView.ItemContainerStyle> 
</ListView> 

は(C#のから)動的にするDataTemplateの幅を変更することです。私はWidthパラメータのData Bindingを試しましたが、うまくいかないようです。 提案がありますか?

+0

「幅」をどのようにバインドしようとしましたか?そして、 'Snoop'やビジュアルスタジオの' output'がバインディングのために示しているのは、何かエラーがありますか? – Shakra

+0

' 'それから、私は.csファイルから設定しようとしました。エラーはありません。 – BlackM

+0

ListViewItemのビューモデルにTemplateWidthプロパティがありましたか? – Shakra

答えて

0

私はあなたが一覧表示移入た方法を知って、そしてどのVM使用するので、私は例として、周りにいくつかの作業を行っていない:

XAML:

<Grid> 
    <Grid.RowDefinitions> 
    <RowDefinition/> 
    <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <ListView Name="lvDataBinding" VerticalAlignment="Top" Margin="0,200,0,30" ScrollViewer.CanContentScroll="False" BorderBrush="Transparent" BorderThickness="0"> 
    <ListView.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal"></StackPanel> 
     </ItemsPanelTemplate> 
    </ListView.ItemsPanel> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <Rectangle Fill="AntiqueWhite" Stroke="Black" StrokeThickness="1" 
         Height="50" 
         Name="dataTemplate" 
         Margin="-5,0,-5,0" 
         Width="{Binding RelativeSource={RelativeSource FindAncestor, 
       AncestorType=Window}, Path=ItemWidth}"/> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="ListViewItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
     </Style> 
    </ListView.ItemContainerStyle> 
    </ListView> 
    <TextBox Grid.Row="1" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, 
    Path=ItemWidth}"/> 
</Grid> 

とCSファイルでこのウィンドウの内容:

public static readonly DependencyProperty ItemWidthProperty = DependencyProperty.Register(
     "ItemWidth", typeof (int), typeof (MainWindow), new PropertyMetadata(170)); 

    public int ItemWidth 
    { 
     get { return (int) GetValue(ItemWidthProperty); } 
     set { SetValue(ItemWidthProperty, value); } 
    }