我有以下 WPF 用戶控制元件:
<UserControl x:Class="myComponents.UI.TextBoxWithPlaceholder"
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:pEp.UI"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Loaded="UserControl_Loaded">
<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:TextBoxWithPlaceholder}}"
Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox Name="myCustomTextBox"
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"
Padding="5"
IsReadOnly="{Binding IsReadOnly}"
HorizontalAlignment="Stretch"
TextChanged="CustomTextBox_TextChanged"
GotFocus="CustomTextBox_GotFocus"
LostFocus="CustomTextBox_LostFocus"
Margin="5"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto" />
<TextBlock Name="myPlaceholderTextBlock"
IsHitTestVisible="False"
Padding="5"
Text="{Binding Placeholder}"
HorizontalAlignment="Left"
Foreground="DarkGray"
Margin="5">
</TextBlock>
</Grid>
</UserControl>
基本上它是一個帶有占位符的文本框。
現在從 WPF 視圖中,我通過執行以下操作重用此組件:
xmlns:ui="clr-namespace:myComponents.UI"
然后將其作為普通控制元件放置:
<ui:TextBoxWithPlaceholder Name="myNewTextBox" IsReadOnly="{Binding IsReadOnly}"
Style="{StaticResource myTextBoxStyle}"
Placeholder="please, enter something here"/>
現在正如你在上面看到的,我為它設定了一個自定義樣式:
<Style x:Key="myTextBoxStyle" TargetType="{x:Type ui:TextBoxWithPlaceholder}">
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Style.Triggers>
<Trigger Property="IsFocused" Value="False">
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="Foreground" Value="{x:Null}"/>
<Setter Property="BorderBrush" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
</Style>
現在,在我的“myNewTextBox”控制元件中,我試圖覆寫名為 myCustomTextBox 和 myPlaceholderTextBlock 的控制元件的一些繼承屬性,例如 Margin、Padding、Background、Foreground、BorderBrush 等,但我嘗試了上述樣式,但它不起作用。我也試過:
<Style x:Key="myTextBoxStyle" TargetType="{x:Type ui:TextBoxWithPlaceholder}">
<Setter Property="{Binding Path=Margin, ElementName=myCustomTextBox}" Value="0" />
<Setter Property="{Binding Path=Padding, ElementName=myCustomTextBox}" Value="0" />
<Setter Property="{Binding Path=Margin, ElementName=myPlaceholderTextBlock }" Value="0" />
<Setter Property="{Binding Path=Padding, ElementName=myPlaceholderTextBlock }" Value="0" />
<Style.Triggers>
<Trigger Property="IsFocused" Value="False">
<Setter Property="{Binding Path=Background, ElementName=myCustomTextBox}" Value="{x:Null}"/>
<Setter Property="{Binding Path=Foreground, ElementName=myCustomTextBox}" Value="{x:Null}"/>
<Setter Property="{Binding Path=BorderBrush, ElementName=myCustomTextBox}" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
</Style>
uj5u.com熱心網友回復:
如果您希望能夠設定使用控制元件的myCustomTextBoxfrom 視圖的屬性TextBoxWithPlaceholder,則應將依賴項屬性添加到后者并在 TextBoxWithPlaceholder.xaml 中系結到它們并在使用視圖中設定它們,例如:
<ui:TextBoxWithPlaceholder ....PlaceHolderMargin="10" />
TextBoxWithPlaceholder.xaml:
<TextBlock Name="myPlaceholderTextBlock"
...
Margin="{Binding PlaceHolderMargin,RelativeSource={RelativeSource AncestorType=UserControl}}">
恐怕您無法ElementName=myPlaceholderTextBlock從控制元件外部的名稱范圍進行參考,TextBoxWithPlaceholder因此嘗試Style在消費視圖中定義的名稱范圍內執行此操作將行不通。
uj5u.com熱心網友回復:
這是一個需要自定義控制元件和視覺狀態而不是UserControl觸發器的任務。但是如果你必須這樣做UserControl(我不怪你,因為在這個階段要學習很多東西),那么這里是:
首先,當你使用ElementName它時,它應該是指 XAML 處理器已經看到的元素,之前在當前的 UI 中被布局。不是控制元件內的元素被設定樣式。我認為這種方法行不通。
如果您希望TextBoxand TextBlockinside aTextBoxWithPlaceholder使用該外部控制元件的屬性,您可以將它們系結到它,在您的控制元件的 XAML 中。例如,要重寫一小部分,即系結背景。
<TextBox Name="myCustomTextBox"
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"
Background={Binding RelativeSource={RelativeSource.FindAncestor, AncestorType={x:Type ui:TextBoxWithPlaceholder}, Path=Background}}"
但是,如果您真的希望嵌套的 TextBox(“myCustomTextBox”)使用帶有觸發器的樣式及其自己的專用屬性值,那么您可能會嘗試Resources在您的樣式中創建一個部分,該部分本身包含TextBox和TextBlock 類似的隱式樣式
<Style x:Key="myTextBoxStyle" TargetType="{x:Type ui:TextBoxWithPlaceholder}">
<Style.Resources>
<!-- Implicit style for TextBox should only apply to TextBoxes inside a TextBoxWithPlaceholder -->
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Style.Triggers>
<Trigger Property="IsFocused" Value="False">
<Setter Property="Background Value="{x:Null}"/>
<Setter Property="Foreground" Value="{x:Null}"/>
<Setter Property="BorderBrush" Value="{x:Null}"/>
</Trigger>
<Style.Triggers>
</Style>
</Style.Resources>
</Style>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/450817.html
上一篇:如何在VM出現V時呼叫重繪任務
