从WPF UserControl的<datatemplate>中访问VM </datatemplate>

时间:2013-02-19 20:58:06

标签: c# wpf xaml user-interface mvvm

我正在尝试使用ViewModel中的属性为WPF Extended ToolKit中的控件BusyIndi​​cator分配文本值。提供的控件示例使用DataTemplate分配进度文本。静态分配文本很好,但是当尝试从VM访问值以分配值时会导致绑定错误。

在app.xaml.cs中处理的VM的分配:

public partial class App : Application {

        private void OnStartup(object sender, StartupEventArgs seArgs) {
            MainWindowView mwv = new MainWindowView();
            mwv.DataContext = new DataSetViewModel(new DataSetModel());
            mwv.Show();
        }
}

View Control无效,但从VM获取的内容字符串除外:

<kit:BusyIndicator IsBusy="{Binding Path=ProcessingData,Converter={StaticResource busyIndicator}}"
                               DisplayAfter="0"
                                           >


    <kit:BusyIndicator.BusyContentTemplate>
        <DataTemplate>
            <StackPanel Margin="4">

                <!-- Output Error is 'WaitProgressUploadTitle' property not found on 'object' ''String' (HashCode=-775806441)'... -->
                <!-- Output Error is  'WaitProgressUploadContent' property not found on 'object' ''String' (HashCode=-775806441)'... -->
                <TextBlock Text="{Binding Path=WaitProgressUploadTitle}" FontWeight="Bold" HorizontalAlignment="Center"/>  
                <StackPanel Margin="4">
                    <TextBlock Text="{Binding Path=WaitProgressUploadContent}" />
                    <ProgressBar Value="40" Height="15"/>
                </StackPanel>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Button Grid.Column="0" Content="Pause" HorizontalAlignment="Right" Margin="0 0 2 0"/>
                    <Button Grid.Column="1" Content="Cancel" HorizontalAlignment="Left" Margin="2 0 0 0"/>
                </Grid>
            </StackPanel>
        </DataTemplate>
    </kit:BusyIndicator.BusyContentTemplate>
    <kit:BusyIndicator.OverlayStyle>
        <Style TargetType="Rectangle">
            <Setter Property="Fill" Value="#ffffeeee"/>
        </Style>
    </kit:BusyIndicator.OverlayStyle>
    <kit:BusyIndicator.ProgressBarStyle>
        <Style TargetType="ProgressBar">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
    </kit:BusyIndicator.ProgressBarStyle>

        <TextBlock Text="CONTENTCONTENTCONTENTCONTENT" Margin="0,100,0,0" HorizontalAlignment="Center" MaxWidth="675" TextAlignment="Center" FontSize="20" Visibility="{Binding Path=FileOpenGood,Converter={StaticResource errorVisibility}}" Background="Cornsilk" />

    </kit:BusyIndicator>

1 个答案:

答案 0 :(得分:0)

使用ContentTemplateDataTemplate绑定时Content的{​​{1}}属性而不是ContentControl

只需设置

DataContext

顺便说一句。从错误中可以看出,绑定对象的类型为BusyContent={Binding}