数据绑定到嵌套的UserControl

时间:2016-07-28 16:46:12

标签: c# wpf xaml user-controls

我想将一个属性从嵌套的UserControl绑定到我的MainWindow。 我已经在其他方面使用了类似的结构,但这有点不同:

  1. -MainWindow
  2. - 织带
  3. --- RibbonButton< - 我要禁用的按钮
  4. - /色带
  5. - UserControl1 - 直接将我想绑定的控件
  6. --- UserControl2 - 嵌套用户控件,坐在上面的
  7. ---- TextBox< - 将RibbonButton绑定到Text属性。 TextBox为空时禁用按钮
  8. --- / UserControl2
  9. - /用户控件
  10. - /主窗口
  11. 因此,Elementname和Path的通常组合显然不起作用。 DataContext是这样的吗? UserControls位于单独的文件中,MainWindow也是如此。或者我应该在代码隐藏中执行此操作吗?

    我在那里的一些代码:

    主窗口     

    <RibbonWindow.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis"/>
    </RibbonWindow.Resources>
    ...
                <RibbonGroup Header="Import">
                    <RibbonButton x:Name="mplImpFromForum" Label="From forum" FontSize="12" LargeImageSource="Icons/General/Icon8_download_72px.png" Click="mplImpFromForum_Click"/>
                    <RibbonButton x:Name="mplImpManual" Label="Manual import" SmallImageSource="Icons/General/Icon8_whole_hand_32px.png"}>
                        <RibbonButton.Style>
                            <Style>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=frmLogin, Path=}"
    // here lies my problem
                                </Style.Triggers>
                            </Style>
                        </RibbonButton.Style>
                    </RibbonButton>
                </RibbonGroup>
            </RibbonTab>
        </Ribbon>
    
        <Viewbox Grid.Row="1" Panel.ZIndex="1" MaxHeight="200" MaxWidth="200">
            <local:frmLogin x:Name="frmLogin" Loaded="frmLogin_Loaded" Grid.Row="1"/>
        </Viewbox>
        <local:frmMPLMain x:Name="frmMPLMain" Grid.Row="1"/> // "Source UserControl, "parent 
    to the real target.
    

    第一个嵌套的UserControl

    <UserControl x:Class="Fever_Tool_WPF.frmMPLMain"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Fever_Tool_WPF"
             mc:Ignorable="d"
             x:Name="MPLRoot"
             d:DesignHeight="500" d:DesignWidth="1000">
    ...
        <local:frmMPLImp x:Name="frmMPLImp" Grid.Column="2" Grid.ColumnSpan="4" Grid.RowSpan="6"/>
    // The second UserControl, this one contains the TextBox I want to take as a source
        <DataGrid HeadersVisibility="None" Grid.RowSpan="3" Grid.ColumnSpan="2"></DataGrid>
    </Grid>
    

    第二个UserControl

    <UserControl x:Class="Fever_Tool_WPF.frmMPLImp"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Fever_Tool_WPF"
             mc:Ignorable="d" 
             d:DesignHeight="500" d:DesignWidth="1000">
    <UserControl.Resources>
        <Style x:Key="uiMPLImpBaseStyle" TargetType="{x:Type FrameworkElement}">
            <Setter Property="Margin" Value="5"/>
        </Style>
    </UserControl.Resources>
    ...
        <TextBox x:Name="txtMPLImport" Style="{StaticResource uiMPLImpBaseStyle}" Grid.Row="2" Grid.ColumnSpan="4"/>
    // This is the textbox I would like to check.
    ...
    </Grid>
    

0 个答案:

没有答案