I have a CustomControl SearchDefalut
There’s an IsShowPopup to display Popuo,
Property
public bool IsShowPopup
{
get { return (bool)GetValue(IsShowPopupProperty); }
set { SetValue(IsShowPopupProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsShowPopupProperty =
DependencyProperty.Register("IsShowSearch", typeof(bool), typeof(SearchDefalut), new PropertyMetadata(true));
Xml
<Style TargetType="{x:Type units:SearchDefalut}">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type units:SearchDefalut}">
<Border Background="{TemplateBinding Background}"
BorderBrush="Yellow"
BorderThickness="2" CornerRadius="10">
<Grid>
<Popup IsOpen="{Binding RelativeSource={RelativeSource Templatedparent}, Path=IsShowPopup}" AllowsTransparency="True" StaysOpen="False"
Width="{TemplateBinding Width}" Height="200">
<Border Background="Gray" Margin="3">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<VirtualizingStackPanel x:Name="PART_ItemPanel" Orientation="Vertical" />
</ScrollViewer>
<Border.Effect>
<DropShadowEffect BlurRadius="8" ShadowDepth="6" Opacity="0.8" Color="Red" />
</Border.Effect>
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="SearchData" Value="">
<Setter Property="Visibility" TargetName="watermark" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The IsOpen of Popup is bound to IsShowPopup. When the program runs, IsOpen renders the effect according to the initial value of the dependent attribute. However, when the control is used to change the value of IsShowPopup, IsOpen cannot be modified, resulting in no PoPup display effect, which makes me very upset
I couldn’t achieve it this way either
IsOpen="{Binding IsShowPopu, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type unit:SearchDefalut}},
UpdateSourceTrigger=PropertyChanged}"
5