绑定用户控件的可见性不会崩溃控件

时间:2015-11-05 11:26:08

标签: c# wpf user-controls

我确实遇到了绑定usercontrol可见性的问题。

对Visibility类型的依赖属性的绑定工作正常,DP保存正确的值(在本例中为Collapsed)。 UserControl中我的网格内容设置为折叠,但孔控制不会崩溃。它仍然保留由with和heigth定义的空间,如xaml中所引用。

编辑:我发现,问题是我在xaml中设置宽度和高度,我引用了我的usercontrol。如果我不这样做,控件就会崩溃正确(因此绑定工作正常)。但我需要设置宽度和高度,以防用户控件可见。

知道如何解决这个问题吗?

<my:MenuButtonBase x:Class="bxSuite.Controls.MenuButtonLarge"
             xmlns:my="clr-namespace:bxSuite.Controls"
             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" 
             Background="Black" 
             >
    <Grid Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=ButtonVisibility}" >
        <StackPanel>
            <Image Source="{Binding ButtonImageSource}" Margin="5,10,5,5" Width="48" Height="48" VerticalAlignment="Top" HorizontalAlignment="Center" />
            <TextBlock Text="{Binding FunctionHeader}" Foreground="White" TextWrapping="Wrap" TextAlignment="Center" Padding="5,5,5,5" FontSize="12" />
        </StackPanel>
    </Grid>
</my:MenuButtonBase>

在XAML中,我像这样引用我的usercontrol(转换器正确生成可见性状态):

<my:MenuButtonLarge Name="btnInEuqipment" ButtonVisibility="{Binding Path=User, Converter={StaticResource ConverterUserRightVisibility}, ConverterParameter=5}" VerticalAlignment="Top" FunctionHeader="{lex:Loc Key=MenuButton_InEquipment}" Width="130" ButtonImageSource="/bxSuite.RolloutManager;component/Images/inequipment_48x48.png" BackgroundEnabled="#FF0694FD" BackgroundHover="#FF0072C6" MenuButtonClick="btnInEuqipment_MenuButtonClick" Height="95" Margin="5,10,0,0" />

1 个答案:

答案 0 :(得分:0)

尝试设置用户控件的可见性而不是网格,应该可行。

您的代码应该是这样的。

<my:MenuButtonBase x:Class="bxSuite.Controls.MenuButtonLarge"
         xmlns:my="clr-namespace:bxSuite.Controls"
         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" 
         Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=ButtonVisibility}" 
         Background="Black">
<Grid>
    <StackPanel>
        <Image Source="{Binding ButtonImageSource}" 
               Margin="5,10,5,5" 
               Width="48" 
               Height="48" 
               VerticalAlignment="Top" 
               HorizontalAlignment="Center" />
        <TextBlock Text="{Binding FunctionHeader}" 
                   Foreground="White" 
                   TextWrapping="Wrap" 
                   TextAlignment="Center" 
                   Padding="5,5,5,5" 
                   FontSize="12" />
    </StackPanel>
</Grid>

别忘了更新绑定的RelativeSource。

相关问题