标签: binding

绑定ElementName.它是使用Visual Tree还是Logical Tree

{Binding ElementName=foo},它会查找视觉或逻辑树吗?

WPF中的逻辑和可视树 数据见,数据做

逻辑树什么时候重要?

当查找名称时,例如在{Binding ElementName = Foo}中,搜索会向祖先寻找名称范围,就像对可继承属性一样.

Silverlight中的ElementName绑定通过附加行为

为了实现这一点,WPF提供了ElementName和RelativeSource绑定,为您提供了一种强大的机制,用于定位可视树中要绑定的其他元素.

编辑:

它看起来像用于绑定的逻辑树ElementName.

争论#1.

根据MSDN文章FrameworkElement类:

FrameworkElement扩展了UIElement并添加了以下功能:

  • 支持数据绑定和动态资源引用:对数据绑定和资源的属性级支持由DependencyProperty类实现并体现在属性系统中,但是能够解析存储为Expression的成员值(编程构造) FrameworkElement实现了数据绑定和动态资源的基础.有关更多信息,请参阅数据绑定概述和资源概述.

争论#2.

ElementName指向x:Name,所以这个名字应该找到一些如何.有一个NameScope概念.

对于大多数情况,在FrameworkElement和FrameworkContentElement上公开的FindName方法是调用按名称搜索元素的更合适的方法.FrameworkElement和FrameworkContentElement公开的Name属性是用于将初始名称设置为标记属性的更合适的属性.并且在FrameworkElement和FrameworkContentElement上公开的RegisterName方法对于在特定的名称范围内建立名称是必要的(没有可以直接执行此操作的NameScope成员;您必须首先将当前的namescope设置为使用RegisterName).

另一方面,Visual类既没有FindName方法也没有实现INameScope.

wpf binding elementname visual-tree logical-tree

20
推荐指数
1
解决办法
1万
查看次数

WPF MVVM从ViewModel在视图上触发事件的正确方法

在我的WPF应用程序中,我有2个Windows(两个Windows都有自己的ViewModel):

  1. 应用程序的主窗口,显示带有一堆单词的列表(绑定到MainViewModel)

  2. 允许用户将新项添加到列表中的对话窗口(绑定到AddWordViewModel)

MainViewModel具有List的Items属性(此集合由其中一个服务类填充)绑定到Main Window的ListBox

AddWordViewModel具有绑定到"添加单词对话框"的"保存"按钮的SaveWordCommand.它的任务是获取用户输入的文本并将其传递给服务类.

用户单击"保存"按钮后,我需要通知MainViewModel从服务中重新加载文章.

我的想法是在MainViewModel中公开public命令并从AddWordViewModel执行它

实施它的正确方法是什么?

谢谢!

.net wpf binding command mvvm

20
推荐指数
1
解决办法
2万
查看次数

条件绑定属性不起作用?

我一直在努力使用这段代码一段时间,似乎无法找到我的问题的任何完整答案.我创建了一个小样本来说明问题:

<ListView >
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
        <StackPanel Margin="0,0,20,0" IsItemsHost="True" />
    </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.Items>
        <TextBlock>Test1</TextBlock>
        <TextBlock>Test2</TextBlock>
        <TextBlock>Test3</TextBlock>
        <TextBlock>Test4</TextBlock>
        <TextBlock>Test5</TextBlock>
    </ListView.Items>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Grid>
                        <ContentPresenter/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                 <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
                                 <Condition Property="IsSelected" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                 </ControlTemplate>
             </Setter.Value>
         </Setter>
         </Style>
     </ListView.ItemContainerStyle>
 </ListView>
Run Code Online (Sandbox Code Playgroud)

根据MultiTrigger设置,当鼠标不再位于所选项目上时,应重新显示所选项目.但是,此代码会生成一个InvalidOperationException,并显示消息"必须具有'Property'的非空值." 如果删除使用"绑定"属性的条件,则不会抛出异常.在MSDN文档中,它声明您必须设置Property或Binding属性.未设置上述代码函数,如Binding属性.实际上,在我的所有测试用例中,Binding属性的设置并不重要; 仍然抛出异常.有什么想法吗?

wpf binding conditional-statements

20
推荐指数
1
解决办法
1万
查看次数

在Windows中使用特定的网络接口作为套接字

除了更改路由表之外,Windows中是否有可靠的方法来强制新创建的套接字使用特定的网络接口?据我所知,bind()接口的IP地址并不能保证这一点.

sockets windows networking binding bind

20
推荐指数
1
解决办法
2万
查看次数

page.DataContext不是从父Frame继承的?

我有一个page框架中的页面frame,有frame.DataContext = "foo".

  • (page.Parent as Frame).DataContext"foo".
  • BindingExpression for page.DataContextis null(也强制使用ClearValue).
  • page.DataContextnull.但我期待"foo"!

为什么DataContext没有继承?据我所知,Frame 沙箱的内容.但是我找不到任何关于这种行为的文件 - 有人能指出我提到这个的地方吗?

c# wpf datacontext msdn binding

20
推荐指数
2
解决办法
7765
查看次数

WCF IncludeExceptionDetailInFaults以编程方式?

我在配置文件中有以下内容,我试图找到C#中的等效位,因为我有一个完全以编程方式配置的服务.我应该寻找什么类/属性/方法?

谢谢.

<behaviors>
    <serviceBehaviors>
        <behavior name="ServiceGatewayBehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
    </serviceBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

wcf binding fault

20
推荐指数
1
解决办法
7304
查看次数

如何在动态创建的ContextMenu中添加水平分隔符?

我在互联网上寻找解决方案,但无法在我的样本中找到它.我需要在从代码隐藏生成的Context菜单项之间添加一个分隔符.我尝试使用如下代码行添加它但没有成功.

this.Commands.Add(new ToolStripSeparator()); 
Run Code Online (Sandbox Code Playgroud)

我想知道是否有人可以提供帮助.先感谢您.

上下文菜单XAML:

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu ItemsSource="{Binding Commands}">
                <ContextMenu.ItemContainerStyle>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Command" Value="{Binding}" />
                        <Setter Property="Header" Value="{Binding Path=Text}" />
                        <Setter Property="CommandParameter" Value="{Binding Path=Parameter}" />
                    </Style>
                </ContextMenu.ItemContainerStyle>
            </ContextMenu>
        </Setter.Value>
    </Setter>
Run Code Online (Sandbox Code Playgroud)

在方法中添加的C#:

this.Commands = new ObservableCollection<ICommand>();
        this.Commands.Add(MainWindow.AddRole1);
        this.Commands.Add(MainWindow.AddRole2);
        this.Commands.Add(MainWindow.AddRole3);
        this.Commands.Add(MainWindow.AddRole4);
        //this.Add(new ToolStripSeparator()); 
        this.Commands.Add(MainWindow.AddRole5);
        this.Commands.Add(MainWindow.AddRole6);
        this.Commands.Add(MainWindow.AddRole7); 
Run Code Online (Sandbox Code Playgroud)

wpf binding wpf-controls wpfdatagrid

20
推荐指数
3
解决办法
2万
查看次数

WPF中的Datagrid绑定

我知道这已经被问到了,但我已经完成了开发人员建议的几乎所有内容.

<DataGrid x:Name="Imported" VerticalAlignment="Top" DataContext="{Binding Source=list}"  AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Path=Date}"/>
    </DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

我试图在模态对话框中显示它并在模式对话框的构造函数中填充许可证列表.但仍然没有任何东西填充在数据网格中.

构造函数代码:

public diagboxclass()
{
    List<object> list = new List<object>();
    list = GetObjectList();
}

public class object
{
    string id;
    DateTime date;
    public string ID
    {
        get { return id; }
        set { id = value; }
    }
    public DateTime Date
    {
        get { return date; }
        set { date = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

你们认为与对象列表有关吗?

c# wpf xaml binding datagrid

20
推荐指数
3
解决办法
16万
查看次数

如何在自定义控件中创建可绑定命令?

假设代码如下,

public class SomeViewModel{
      ICommand ReloadCommand{get...}
      ICommand SaveCommand{get..}
}

//SomeView.xaml
<SomeCustomControl Reload="ReloadCommand" Save="SaveCommand" /> //NOT SURE HOW??

//SomeCustomContro.xaml
<SomeCustomControl x:Name="someCustomControl">
<Button Command={Binding ElementName=someCustomControl, Path=Reload />
<Button Command={Binding ElementName=someCustomControl, Path=Save />
</SomeCustomControl>

//SomeCustomControl.xaml.cs
.....  //NOT SURE HOW TO ALLOW BINDING TO A ICOMMAND ??
Run Code Online (Sandbox Code Playgroud)

在我的SomeCustomControl中,我需要支持"在xaml中绑定ICommand".我理解DependencyProperties可以像这样绑定,但在这种情况下我需要绑定ICommand.

有人可以建议最好的方法是什么?任何建议的材料或链接都是有用的,因为我错过了方向.

编辑1)我可以在SomeCustomControl中使用DataContext SomeView.两者之间有更多的逻辑和分离,我无法解散.我必须在SomeCustomControl的某个地方维护一个Reload/Save ICommands的引用.

非常感谢.

wpf xaml binding

20
推荐指数
1
解决办法
2万
查看次数

wpf eventsetter处理程序绑定样式

我有一个风格,我希望将命令绑定到EventSetterHandlerRelativeSource.该命令位于viewModel中.

<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
    <EventSetter Event="MouseLeftButtonDown" 
                 Handler="{Binding TextBlockMouseLeftButtonDownCommand, 
                           RelativeSource={RelativeSource Self}}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

问题是我得到一个错误,因为这有什么问题(也许不可能以这么简单的方式做到这一点)

我之前搜索了很多,我发现了AttachedCommandBehaviour,但我认为它不适用于风格.

你能给出一些如何解决这个问题的提示吗?

更新13/10/2011

我在MVVM Light Toolkit EventToCommand示例程序中找到了这个:

        <Button Background="{Binding Brushes.Brush1}"
            Margin="10"
            Style="{StaticResource ButtonStyle}"
            Content="Simple Command"
            Grid.Row="1"
            ToolTipService.ToolTip="Click to activate command">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <cmd:EventToCommand Command="{Binding SimpleCommand}" />
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseLeave">
                <cmd:EventToCommand Command="{Binding ResetCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
Run Code Online (Sandbox Code Playgroud)

但在这里,绑定不是风格.我怎么能把它放到EventToCommand按钮的样式?

c# wpf binding

20
推荐指数
2
解决办法
6万
查看次数