将ControlTemplate中的Button Button命令从DataTemplate中的Element绑定到SelectedItem

时间:2015-06-23 14:24:47

标签: wpf binding command datatemplate controltemplate

我已经做了一些谷歌搜索和搜索,并在StackOverFlow上发现了许多问题,这些问题接近帮助我但是他们的所有想法仍然以一种或另一种形式给出了绑定表达错误,大多数人总是提到它无法找到源。< / p>

Cannot find source for binding with reference 'ElementName=filesList'.

查看调用控件模板:

<Expander
                DockPanel.Dock="Top"
                SnapsToDevicePixels="True"
                Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="3" Grid.RowSpan="1"
                Header="Files"
                IsExpanded="True"
                ExpandDirection="Down"
                Background="{DynamicResource DynamicFrmBG}" Foreground="{DynamicResource DynamicFrmFG}"
                >
                <ContentControl
                    SnapsToDevicePixels="True"
                    Template="{StaticResource filesTemplate}"
                    />
            </Expander>

控制模板代码片段(此代码中的注释是我尝试过的另一个不同的例子):

<ControlTemplate x:Key="filesTemplate">
    <Grid
        SnapsToDevicePixels="True"
        >
        <Grid.RowDefinitions>
            <RowDefinition Height="5" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="5" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="5" />
        </Grid.ColumnDefinitions>

       <ContentControl
            SnapsToDevicePixels="True"
            Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="2" Grid.RowSpan="1"
            Margin="0,0,0,0"
            Content="{Binding Files}"
            ContentTemplate="{StaticResource filesListTemplate}"
            />

        <StackPanel
            SnapsToDevicePixels="True"
            Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="3" Grid.RowSpan="1"
            Orientation="Horizontal"
            >
            <Button
                SnapsToDevicePixels="True"
                Foreground="{DynamicResource DynamicFrmFG}"
                Margin="0,5,5,5"
                Content="Download"
                Command="{Binding SelectedItem.Content.DownloadCommand, ElementName=filesList}"
                MinWidth="50" 
                >
                <!--Command="{Binding SelectedItem.Content.DownloadCommand, Source={StaticResource ResourceKey=filesListTemplate}, ElementName=filesList}"-->
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="{DynamicResource DynamicFrmBGColor}" Offset="0"/>
                        <GradientStop Color="{DynamicResource DynamicFrmFGColor}" Offset="2"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
        </StackPanel>
    </Grid>

DataTemplate的代码片段(ListView数据源设置为ObservableCollection,FilesViewModel包含Download命令):

<DataTemplate x:Key="filesListTemplate">
    <ListView
        x:Name="filesList"
        SnapsToDevicePixels="True"
        IsTextSearchEnabled="False"
        ItemsSource="{Binding }" 
        HorizontalContentAlignment="Stretch"
        BorderBrush="{DynamicResource DynamicFrmFG}" Foreground="{DynamicResource DynamicFrmFG}" Background="{DynamicResource DynamicFrmBG}"
        ItemContainerStyle="{DynamicResource ListViewItemStyle_Gray}"
        >
        <ListView.View>
            <GridView 
                    AllowsColumnReorder="False"
                    ColumnHeaderContainerStyle="{DynamicResource SpecialityHeader}"
                    >

                <GridViewColumn
                        Width="200" Header="ID">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock 
                                    SnapsToDevicePixels="True"
                                    FontFamily="Consolas" FontSize="14"
                                    Text="{Binding ID}" 
                                    />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn
                        Width="200" Header="File Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock 
                                    SnapsToDevicePixels="True"
                                    FontFamily="Consolas" FontSize="14"
                                    Text="{Binding Filename}" 
                                    />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn
                        Width="250" Header="Uploaded By">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock 
                                    SnapsToDevicePixels="True"
                                    FontFamily="Consolas" FontSize="14"
                                    Text="{Binding Role}" 
                                    />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn
                        Width="200" Header="Uploaded When">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock 
                                    SnapsToDevicePixels="True"
                                    FontFamily="Consolas" FontSize="14"
                                    Text="{Binding UploadedWhen}" 
                                    />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</DataTemplate>

我想做的是这个;从ListView,download命令将我的控件模板中的按钮绑定到选定的FileViewModel。但是,此命令位于DataTemplate中。有人可以帮我成功吗?

在1705EST编辑:添加了调用控件模板的视图。

0 个答案:

没有答案