如何找到Binded属性的来源

时间:2013-06-21 06:45:31

标签: .net xaml mvvm

这可能是一个非常基本的问题,我是Silverlight和MVVM的新手,

我有一个按钮

  <Button  IsEnabled="{Binding IsUpdatingStatus}"> </Button>

只是想知道我是否必须找到IsUpdatingStatus的源代码(在哪个视图模型中存在) 我在哪里可以看到从Xaml开始的方向

编辑:

我的xaml有这样的标记:

  <UserControl x:Class="LMS.Client.View.LeadStatusControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
        xmlns:vc="clr-namespace:LMS.Client.Controls"
        xmlns:control="clr-namespace:LMS.Client.Controls;assembly=LMS.Client.Common"
        xmlns:commanding="clr-namespace:LMS.Client.Commanding;assembly=LMS.Client.Common"
        mc:Ignorable="d">

        <ContentControl Style="{StaticResource Content.ContentControlStyle}">

    .....

如果我遗失了什么,请告诉我,告诉我我是不是错了

this is actually complete Xaml

:



<UserControl x:Class="LMS.Client.View.LeadStatusControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    xmlns:vc="clr-namespace:LMS.Client.Controls"
    xmlns:control="clr-namespace:LMS.Client.Controls;assembly=LMS.Client.Common"
    xmlns:commanding="clr-namespace:LMS.Client.Commanding;assembly=LMS.Client.Common"
    mc:Ignorable="d">

    <ContentControl Style="{StaticResource Content.ContentControlStyle}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <Border BorderThickness="2" BorderBrush="Silver" CornerRadius="8" Margin="10,0,10,20" Grid.Row="1">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="2"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid Grid.Column="1" Background="Silver" Margin="0,30,0,30"/>

                    <Grid Grid.Column="0" Margin="5,0,5,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="100"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Style="{StaticResource Content.LabelTextStyle}" Text="Update Status" HorizontalAlignment="Center"/>

                        <TextBlock Grid.Column="0" Grid.Row="1" Text="Lead Status" Style="{StaticResource Content.LabelTextStyle}"/>
                        <ComboBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding UpdatingStatus}" Style="{StaticResource SemiComboBoxStyle}"
                                  Margin="5,0,5,0" DisplayMemberPath="StatusCode" SelectedItem="{Binding SelectedUpdatingStatus,Mode=TwoWay}" IsEnabled="{Binding IsStatusEditable}"/>

                        <TextBlock Grid.Column="0" Grid.Row="2" Text="Reason" Style="{StaticResource Content.LabelTextStyle}"/>
                        <ComboBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding LeadStatusReasons}" Style="{StaticResource SemiComboBoxStyle}"
                                  Margin="5,0,5,0" DisplayMemberPath="Description" SelectedItem="{Binding SelectedStatusReason,Mode=TwoWay}" IsEnabled="{Binding IsStatusEditable}"/>

                        <TextBlock Grid.Column="0" Grid.Row="3" Text="Comments" Style="{StaticResource Content.LabelTextStyle}"/>
                        <TextBox Grid.Column="1" Grid.Row="3" IsEnabled="{Binding IsStatusEditable}" Text="{Binding Entity.DisqualificationComments,Mode=TwoWay}" Margin="5,0,5,0" Height="100" Style="{StaticResource SemiTextBoxStyle}" TextWrapping="Wrap" AcceptsReturn="True"/>

                        <TextBlock Grid.Column="0" Grid.Row="4" Text="CS Number" Style="{StaticResource Content.LabelTextStyle}"/>
                        <TextBox Grid.Column="1" Grid.Row="4" IsEnabled="{Binding IsStatusEditable}" Text="{Binding Entity.CSNumber,Mode=TwoWay,NotifyOnValidationError=True, ValidatesOnExceptions=True}" Margin="5,0,5,0" Style="{StaticResource SemiTextBoxStyle}"/>

                        <TextBlock Grid.Column="0" Grid.Row="5" Text="Contract No" Style="{StaticResource Content.LabelTextStyle}"/>
                        <TextBox Grid.Column="1" Grid.Row="5" IsEnabled="{Binding IsStatusEditable}" Text="{Binding Entity.ContractNo,Mode=TwoWay}" Margin="5,0,5,0" Style="{StaticResource SemiTextBoxStyle}"/>

                        <TextBlock Grid.Column="0" Grid.Row="6" Text="Site No" Style="{StaticResource Content.LabelTextStyle}"/>
                        <TextBox Grid.Column="1" Grid.Row="6" IsEnabled="{Binding IsStatusEditable}" Text="{Binding Entity.SiteNo,Mode=TwoWay}" Margin="5,0,5,0" Style="{StaticResource SemiTextBoxStyle}"/>

                        <TextBlock Grid.Column="0" Grid.Row="7" Text="Schedule Date" Style="{StaticResource Content.LabelTextStyle}"/>
                        <control:CustomizedDatePicker Grid.Column="1" Grid.Row="7" Margin="5,0,5,0" Style="{StaticResource SemiDatePickerStyle}" Width="200" 
                                    SelectedDate="{Binding Entity.ScheduleDate,Mode=TwoWay}" HorizontalAlignment="Left" IsEnabled="{Binding IsStatusEditable}"/>

                        <TextBlock Grid.Column="0" Grid.Row="8" Text="Reschedule Date" Style="{StaticResource Content.LabelTextStyle}"/>
                        <control:CustomizedDatePicker Grid.Column="1" Grid.Row="8" Margin="5,0,5,0" Style="{StaticResource SemiDatePickerStyle}" Width="200" 
                                    SelectedDate="{Binding Entity.RescheduledDate,Mode=TwoWay}" HorizontalAlignment="Left" IsEnabled="{Binding IsStatusEditable}"/>

                        <TextBlock Grid.Column="0" Grid.Row="9" Text="Followup Date" Style="{StaticResource Content.LabelTextStyle}"/>
                        <control:CustomizedDatePicker Grid.Column="1" Grid.Row="9" Margin="5,0,5,0" Style="{StaticResource SemiDatePickerStyle}" Width="200" 
                                    SelectedDate="{Binding Entity.FollowupDate,Mode=TwoWay}" HorizontalAlignment="Left" IsEnabled="{Binding IsStatusEditable}"/>

                        <TextBlock Grid.Column="0" Grid.Row="10" Text="Installed Date" Style="{StaticResource Content.LabelTextStyle}"/>
                        <control:CustomizedDatePicker Grid.Column="1" Grid.Row="10" Margin="5,0,5,0" Style="{StaticResource SemiDatePickerStyle}" Width="200" 
                                    SelectedDate="{Binding Entity.InstalledDate,Mode=TwoWay}" HorizontalAlignment="Left" IsEnabled="{Binding IsStatusEditable}"/>

                        <TextBlock Grid.Column="0" Grid.Row="11" Text="Online Date" Style="{StaticResource Content.LabelTextStyle}"/>
                        <control:CustomizedDatePicker Grid.Column="1" Grid.Row="11" Margin="5,0,5,0" Style="{StaticResource SemiDatePickerStyle}" Width="200" 
                                    SelectedDate="{Binding Entity.OnlineDate,Mode=TwoWay}" HorizontalAlignment="Left" IsEnabled="{Binding IsStatusEditable}"/>

                        <TextBlock Grid.Column="0" Grid.Row="12" Text="Purchasing Submitted Date" Style="{StaticResource Content.LabelTextStyle}"/>
                        <control:CustomizedDatePicker Grid.Column="1" Grid.Row="12" Margin="5,0,5,0" Style="{StaticResource SemiDatePickerStyle}" Width="200" 
                                    SelectedDate="{Binding Entity.PurchasingSubmittedDate,Mode=TwoWay}" HorizontalAlignment="Left" IsEnabled="{Binding IsStatusEditable}"/>

                        <TextBlock Grid.Column="0" Grid.Row="13" Text="Purchased Date" Style="{StaticResource Content.LabelTextStyle}"/>
                        <control:CustomizedDatePicker Grid.Column="1" Grid.Row="13" Margin="5,0,5,0" Style="{StaticResource SemiDatePickerStyle}" Width="200"
                                    SelectedDate="{Binding Entity.PurchasedDate,Mode=TwoWay}" HorizontalAlignment="Left" IsEnabled="{Binding IsStatusEditable}"/>

                    </Grid>

                    <Grid Grid.Column="2" Margin="5,0,5,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <TextBlock Grid.Column="0" Grid.Row="0" Style="{StaticResource Content.LabelTextStyle}" Text="Disqualifications" HorizontalAlignment="Center"/>

                        <ListBox Margin="10,0,0,0" Grid.Row="1" Grid.Column="0" Style="{Binding SemiListBoxStyle}" 
                                 ItemsSource="{Binding DisqualificationReasons}" IsEnabled="{Binding IsStatusEditable}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <CheckBox ClickMode="Press" IsChecked="{Binding IsSelected,Mode=TwoWay}" IsEnabled="{Binding IsEnabled}">
                                        <CheckBox.Content>
                                            <TextBlock Text="{Binding Reason.DisplayText}" Style="{StaticResource Content.LabelTextStyle}" />
                                        </CheckBox.Content>
                                    </CheckBox>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>
                </Grid>
            </Border>

            <Grid Grid.Row="2">
                <StackPanel HorizontalAlignment="Left" Margin="10,20,10,20" Orientation="Horizontal">
                    <Button Style="{StaticResource ButtonStyle}" Width="80"
                                    Command="{Binding SubmitUpdateStatusCommand}" IsEnabled="{Binding IsUpdatingStatus}">
                        <TextBlock Text="Update" Style="{StaticResource Button.TextStyle}"/>
                    </Button>
                    <Button Style="{StaticResource ButtonStyle}" Width="80" Margin="12,0,0,0"
                                    Command="{Binding CancelCommand}" IsEnabled="{Binding CanCancel}">
                        <TextBlock Text="{StaticResource Common.Cancel}" Style="{StaticResource Button.TextStyle}"/>
                    </Button>
                </StackPanel>
            </Grid>
        </Grid>
    </ContentControl>

现在可以告诉我标记的位置在哪里

2 个答案:

答案 0 :(得分:0)

Button的DataContext属性将为您提供绑定属性的来源。

答案 1 :(得分:0)

要使数据绑定工作,您需要将DataContext设置为ViewModel。您可以通过各种方式执行此操作。例如,在页面的构造函数中:

Page.xaml.cs

public Page()
{
    DataContext = new PageViewModel();
}

如果在PageViewModel中有一个名为IsUpdatingStatus的布尔属性,该按钮将根据IsUpdatingStatus的设置更新其状态。

有些人不喜欢使用构造函数来设置DataContext。您也可以在XAML文件中静态设置它,也可以查看this question的答案,或使用OnNavigatedTo事件处理程序:

protected override void OnNavigatedTo(NavigationEventArgs e) { }

我希望(部分)回答你的问题。