Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF UserControl透明,与后台属性无关_C#_Wpf - Fatal编程技术网

C# WPF UserControl透明,与后台属性无关

C# WPF UserControl透明,与后台属性无关,c#,wpf,C#,Wpf,我想创建一个具有透明背景的控件,但我希望background属性定义一些内部控件background。这样我就可以使用背景属性了 <UserControl x:Class="HLab.Base.DateEx" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://sch

我想创建一个具有透明背景的控件,但我希望background属性定义一些内部控件background。这样我就可以使用背景属性了

<UserControl 
x:Class="HLab.Base.DateEx"
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:base="clr-namespace:HLab.Base"
mc:Ignorable="d" Background="Transparent">
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Calendar.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <Style TargetType="base:NumTextBox">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="BorderBrush" Value="#80000000"/>

        </Style>
        <Style TargetType="Label">
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </ResourceDictionary>

</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="1">
        <base:NumTextBox 
            x:Name="TextDay"
            MinValue="0"
            MaxValue="31"
            Background="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Background, Mode=OneWay}"
            ValueChanged="OnValueChange"
        /><Popup 
            Name="Popup" 
            StaysOpen="False">
            <Calendar 
                Name="Calendar" 
                SelectedDatesChanged="Calendar_OnSelectedDatesChanged"/>
        </Popup>
    </Grid>

    <base:NumTextBox 
        Margin ="3,0,0,0"
        Background="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Background, Mode=OneWay}"
        x:Name="TextMonth" 
        Grid.Column="2"
        MinValue="0"
        MaxValue="12"
        ValueChanged="OnValueChange"
        />

    <base:NumTextBox 
        Margin ="3,0,0,0"
        Background="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Background, Mode=OneWay}"
        x:Name="TextYear" 
        Grid.Column="3"
        MinValue="0"
        MaxValue="9999"
        HorizontalContentAlignment="Center"
        ValueChanged="OnValueChange"
        />

    <Button 
        Margin ="3,0,0,0"
        HorizontalAlignment="Left"
        Grid.Column="4"
        Name="Button" 
        Width="15"
        Click="Button_OnClick"
        Background="Transparent">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid Background="Transparent">
                    <ContentControl Content="{StaticResource Calendar}"/>
                </Grid>
           </ControlTemplate>
        </Button.Template>
    </Button>
    <Border  
        x:Name="Mandatory" 
        Grid.Row="0" 
        Grid.Column="1"  
        Visibility="Collapsed" 
        Grid.ColumnSpan="3" Background="#10FF0010" IsHitTestVisible="False" BorderThickness="1" Opacity="0.5" BorderBrush="DarkRed"/>
        <ContentControl x:Name="IconMandatory" 
            Grid.Row="0" Visibility="Collapsed" 
            Grid.Column="0"  
            Margin="5,0,0,0" 
            IsHitTestVisible="False" 
            Content="{StaticResource Edit}" Foreground="DarkRed" Height="15"/>
</Grid>
通常我只是将后台绑定到UserControl后台。但是如何使整个控件背景透明,而不考虑背景属性

<UserControl 
x:Class="HLab.Base.DateEx"
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:base="clr-namespace:HLab.Base"
mc:Ignorable="d" Background="Transparent">
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Calendar.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <Style TargetType="base:NumTextBox">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="BorderBrush" Value="#80000000"/>

        </Style>
        <Style TargetType="Label">
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </ResourceDictionary>

</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="1">
        <base:NumTextBox 
            x:Name="TextDay"
            MinValue="0"
            MaxValue="31"
            Background="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Background, Mode=OneWay}"
            ValueChanged="OnValueChange"
        /><Popup 
            Name="Popup" 
            StaysOpen="False">
            <Calendar 
                Name="Calendar" 
                SelectedDatesChanged="Calendar_OnSelectedDatesChanged"/>
        </Popup>
    </Grid>

    <base:NumTextBox 
        Margin ="3,0,0,0"
        Background="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Background, Mode=OneWay}"
        x:Name="TextMonth" 
        Grid.Column="2"
        MinValue="0"
        MaxValue="12"
        ValueChanged="OnValueChange"
        />

    <base:NumTextBox 
        Margin ="3,0,0,0"
        Background="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Background, Mode=OneWay}"
        x:Name="TextYear" 
        Grid.Column="3"
        MinValue="0"
        MaxValue="9999"
        HorizontalContentAlignment="Center"
        ValueChanged="OnValueChange"
        />

    <Button 
        Margin ="3,0,0,0"
        HorizontalAlignment="Left"
        Grid.Column="4"
        Name="Button" 
        Width="15"
        Click="Button_OnClick"
        Background="Transparent">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid Background="Transparent">
                    <ContentControl Content="{StaticResource Calendar}"/>
                </Grid>
           </ControlTemplate>
        </Button.Template>
    </Button>
    <Border  
        x:Name="Mandatory" 
        Grid.Row="0" 
        Grid.Column="1"  
        Visibility="Collapsed" 
        Grid.ColumnSpan="3" Background="#10FF0010" IsHitTestVisible="False" BorderThickness="1" Opacity="0.5" BorderBrush="DarkRed"/>
        <ContentControl x:Name="IconMandatory" 
            Grid.Row="0" Visibility="Collapsed" 
            Grid.Column="0"  
            Margin="5,0,0,0" 
            IsHitTestVisible="False" 
            Content="{StaticResource Edit}" Foreground="DarkRed" Height="15"/>
</Grid>

如果我使用它:

<DateEx Background="Red"/>


我希望按钮不显示红色背景,而只显示numTextBox。

如果要更改类似内容,必须修改控件的
控件模板

UserControl
通常在您只有一组现有控件需要通过一点逻辑组合在一起时使用。但是,当您需要更多的可定制性时,您需要所谓的“自定义控件”。“自定义控件”直接继承自
控件
(而不是
用户控件
),并要求您使用
控件模板创建默认的
样式

完整的官方文件在这里:。有很多,但也许你可以挑选出你真正需要的信息和例子。

对于更简单的演练,还有以下内容:(向下滚动到“自定义控件”标题)。

创建自定义模板或使用其他属性。我们需要查看完整的代码示例。我添加了一个示例,另一个属性将破坏所有使用此属性的代码。