开发者

WPF binding to label doesn't working?

开发者 https://www.devze.com 2023-02-18 23:12 出处:网络
There is a grid view that listing something from a class property.Class havinf four property, outof which three I m binding to 3 columns of grid view and last property I binded to another label.

There is a grid view that listing something from a class property. Class havinf four property, outof which three I m binding to 3 columns of grid view and last property I binded to another label.

The grid view is working and binding the values but Lable is not... What is the reason?

public class ProjectData
{
     public string ProjectName {get;set;}
        public string MachineName {get;set;}
        public string MachineNumber {get;set;}
        public string StatusDetail {get;set;}
}

Here is xaml:

 <ListView 
            Grid.Row="1" 
            Grid.ColumnSpan="2" 
            Name="PrjtListView" 
            HorizontalContentAlignment="Stretch" 
            FontSize="15" Background="#FFF2F2B7" 
            BorderThickness="1"
            ItemsSource="{Binding ProjectData}">
            <ListView.View>
                <GridView AllowsColumnReorder="True" 
                      ColumnHeaderToolTip="appconfig servers"
                      ScrollViewer.CanContentScroll="True">
                     <GridViewColumn Header="Project Name" Width="400"
                                    DisplayMemberBinding="{Binding ProjectName}" />
                    <GridViewColumn Header="Build Machine" Width="150"
                                    DisplayMemberBinding="{Binding MachineName}"/>
                    <GridViewColumn Header="Port number" Width="120" 
     开发者_运维问答                           DisplayMemberBinding="{Binding MachineNumber}"/>
                </GridView>
            </ListView.View>
        </ListView>
        <UniformGrid Grid.Row="2" Grid.Column="1" VerticalAlignment="Center">
            <Button Content="add to cctray" Margin="0,0,5,0" 
                    Command="{Binding AddToCCTray}"/>
        </UniformGrid>
        **<Label Grid.Row="2" Grid.Column="0" Name="lblTotalProjects" FontSize="12" FontStyle="Italic"
Content="{Binding StatusDetail}"></Label>**  
                 -->> This is not working

This is just code snippet,I will give other details if required.

EDIT: Can we bind the same datacontext twice in same xaml file. One for some other controls and one for some other controls


I suspect that you set the value of the StatusDetail-property later than the controls are created.
Because auto-properties do not support any change notification, the label will not know that there is some data and therefore does not show any content.
Try implementing INotifyPropertyChanged for your properties. This will probably fix your problem.


It may be the reason that you define ItemsSource="{Binding ProjectData}" within ListView scope and your Label doesen't know its Binding source. Set "DataContext" of your View to "ProjectData" instance this might help.


The label is outside of the ListView, and therefore has a different DataContext than the rows of your grid.

I am guessing that your current DataContext has a IList<ProjectData> called ProjectData, otherwise the ItemsSource binding would fail. If that's true, for which item should StatusDetail be displayed? If this label should appear in every grid row, once for each item, you'd need to create a new column for that.

Also, have a look at your output window. Binding error give good error messages there.


I always set my binding to {Binding Path="StatusDetail"}

Hope that helps a bit for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消