绑定WPF用户控件

时间:2013-10-22 04:41:46

标签: c# wpf xaml mvvm

我正在使用wpf中的MVVm开发一个应用程序。 在这里,我必须将用户控件放在另一个用户控件上。

主用户控件将具有用于保存信息的UI元素。另外,我需要将用户控件放在我有列表视图的位置。我需要将数据填充到列表视图中。

XAML如下

<UserControl x:Class="SMTF.TestCaseView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SMTF" mc:Ignorable="d" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    d:DesignHeight="550" d:DesignWidth="508">

<UserControl.Resources>
    <Style x:Key="SMToggle" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}" >
                    <Border CornerRadius="3" Background="{TemplateBinding Background}">
                        <ContentPresenter Margin="3" 
                        HorizontalAlignment="Center" 
                        VerticalAlignment="Center"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Content" Value="Show View"/>
                            <Setter Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <GradientStop Color="#FFF3F3F3" Offset="1"/>
                                        <GradientStop Color="#FFF3F3F3" Offset="0.307"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="False">
                            <Setter Property="Content" Value="Add New"/>
                            <Setter Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <GradientStop Color="#FFF3F3F3" Offset="1"/>
                                        <GradientStop Color="#FFF3F3F3" Offset="0.307"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<Grid Height="550" Width="508">

    <Button Content="Save" Command="{Binding SaveData}" Height="23" HorizontalAlignment="Left" Margin="299,507,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" />
    <Button Content="Reset" Command="{Binding ClearData}" Height="23" HorizontalAlignment="Right" Margin="0,507,47,0" Name="btnReset" VerticalAlignment="Top" Width="75" />

     <Label Content="Category" Height="28" HorizontalAlignment="Left" Margin="13,27,0,0" Name="lblCategory" VerticalAlignment="Top" />
    <ComboBox DisplayMemberPath="Category_Desc" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="343" Margin="118,27,0,0" Name="cboCategory" 
              ItemsSource="{Binding Path=Category}" 
              SelectedValue="{Binding Path=Category_Id}" 
              SelectedValuePath="Category_Id" 
              Text="{Binding Category_Desc}"  >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding CategorySelected}"
                                   CommandParameter="{Binding SelectedValue, ElementName=cboCategory}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </ComboBox>
    <Label Content="Sub Category" Height="28" HorizontalAlignment="Left" Margin="13,65,0,0" Name="lblSubCategory" VerticalAlignment="Top" />
    <ComboBox DisplayMemberPath="Sub_Category_Desc" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="343" Margin="118,65,0,0" Name="cboSubCategory" 
              ItemsSource="{Binding Path=SubCategory}" 
              SelectedValue="{Binding Path=Sub_Category_Id}" 
              SelectedValuePath="Sub_Category_Id" 
              Text="{Binding Sub_Category_Desc}"  >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding SubCategorySelected}"
                                   CommandParameter="{Binding SelectedValue, ElementName=cboSubCategory}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </ComboBox>

    <Label Content="Scenario" Height="28" HorizontalAlignment="Left" Margin="13,101,0,0" Name="lblScenario" VerticalAlignment="Top" />
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="118,101,0,0" Name="cboScenario" VerticalAlignment="Top" Width="343"
             ItemsSource="{Binding Path=Scenario}"
             DisplayMemberPath="Scenario_Desc"
             SelectedValuePath="Scenario_Id"
             SelectedValue="{Binding Path=Scenario_Id}"
             Text="{Binding Scenario_Desc}"
              />

    <TextBox Height="118" HorizontalAlignment="Left" Margin="118,139,0,0" Name="txtCulture" Text="{Binding Test_Desc}" VerticalAlignment="Top" Width="340" />
    <Label Content="Test Description" Height="28" HorizontalAlignment="Left" Margin="13,137,0,0" Name="lblCulture" VerticalAlignment="Top" />
    <Label Content="Applicable to" Height="28" HorizontalAlignment="Left" Margin="13,280,0,0" Name="lblVersion" VerticalAlignment="Top" />
     <ComboBox Height="23" HorizontalAlignment="Left" Margin="118,285,0,0" Name="Version" VerticalAlignment="Top" Width="343"
             ItemsSource="{Binding Path=Version}"
             DisplayMemberPath="Version_Desc"
             SelectedValuePath="Version_Id"
             SelectedValue="{Binding Path=Version_Id}"
             Text="{Binding Version_Desc}"
              />
    <CheckBox Content="Activate" Height="16" HorizontalAlignment="Left" IsChecked="{Binding Active}" Margin="402,263,0,0" Name="chkActive" VerticalAlignment="Top" />
    <local:TestScriptListView HorizontalAlignment="Left" Margin="118,349,0,0" x:Name="scriptList" VerticalAlignment="Top" Height="148" Width="343"  DataContext="{Binding Path=Script}" />
    <Label Content="Script" Height="28" HorizontalAlignment="Left" Margin="13,318,0,0" Name="lblScript" VerticalAlignment="Top" />
    <TextBlock HorizontalAlignment="Left" Margin="118,118,0,0" Height="32">
                    <ToggleButton  x:Name="VisibilityToggle" Focusable="False" Command ="{Binding ShowNew}" Style="{StaticResource SMToggle}" >
                        <Image Source="/Image/Add.png"  Width="16" Height="16" />
                    </ToggleButton>

    </TextBlock>
</Grid>
</UserControl>

如何解决这个问题?

感谢 NS

1 个答案:

答案 0 :(得分:0)

在条目用户控件中添加Listview用户控件的引用并设置Datacontext。 像 -

< StackPanel>

< views:ExceptionStrip DataContext="{Binding ExceptionViewModel}"/>

< /StackPanel>