簡體   English   中英

綁定WPF用戶控件

[英]Binding WPF User Control

我正在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

在條目usercontrol中添加引用Listview的Usercontrol並設置Datacontext。 喜歡-

< StackPanel>

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

< /StackPanel>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM