关于数据绑定的问题

daview daview 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
写了个UI,设置了相关对象,希望通过代码在设置UI对象的DataContext对象的时候,能执行绑定数据,结果失败。

请问有什么办法解决?

我的XAML代码如下


<StackPanel VerticalAlignment="Stretch" Grid.Column="1" Width="Auto" Orientation="Horizontal" x:Name="currentUserInfo">
<ContentControl Content="当前用户:" VerticalContentAlignment="Center" />
<Button Width="Auto" VerticalAlignment="Stretch" Click="ShowContextMenu">
<StackPanel VerticalAlignment="Stretch" Background="#0ff0" Width="Auto" Orientation="Horizontal">
<ContentControl x:Name="currentCorp" VerticalContentAlignment="Center" Content="{Binding CorpName}" />
<ContentControl Content="↑" VerticalContentAlignment="Center" />
</StackPanel>
<ToolTipService.ToolTip>{Binding CorpNotifyInfo}</ToolTipService.ToolTip>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu HorizontalAlignment="Left" VerticalAlignment="Top" >
<toolkit:MenuItem Header="更换公司" Command="{Binding MenuItemCommand}" CommandParameter="{Binding ChangedCorp}" Visibility="{Binding MoreCorps}" />
<toolkit:Separator Visibility="{Binding MoreCorps}" />
<toolkit:MenuItem Header="重新加载" Command="{Binding MenuItemCommand}" CommandParameter="{Binding ReloadCorp}" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Button>
<ContentControl Content="→" VerticalContentAlignment="Center" />
<Button Width="Auto" VerticalAlignment="Stretch" Click="ShowContextMenu">
<StackPanel VerticalAlignment="Stretch" Width="Auto" Orientation="Horizontal" >
<ContentControl x:Name="currentUser" VerticalContentAlignment="Center" Content="{Binding UserName}" />
<ContentControl Content="↑" VerticalContentAlignment="Center" />
</StackPanel>
<ToolTipService.ToolTip>{Binding UserNotifyInfo}</ToolTipService.ToolTip>
</Button>
</StackPanel>



CS代码如下(在Loaded事件的时候执行):


var user = this.CurrentUser;
if (user != null)
{
var userName = user.ExtInfo.Name;
var corp = (from c in user.UserCorpList where c.Selected select c.CorpInfo).SingleOrDefault();
var corpName = corp.Name_S ?? "OA系统";
var corpFullName = corp.Name_L ?? "OA系统";
var data = new
{
CorpNotifyInfo = corpFullName,
UserName = userName,
CorpName = corpName,
MoreCorps = user.UserCorpList.Count > 1 ? Visibility.Collapsed : Visibility.Visible,
ChangeCorp = MenuCommand.ChangeCorp,
ReloadCorp = MenuCommand.ReloadCorp
};
this.currentUserInfo.DataContext = data;
}
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
daview
WPF/Silverlight 8,757 成员 12,031 内容