我對 WPF 比較陌生,并且正在嘗試了解附加屬性以在我用作學習基礎的測驗應用程式中使用它們。AP 位于它自己的名為 AP.cs 的源檔案中,該檔案位于 namespace 中WPFPages.ViewModels。
所以我創建了一個簡單的 AP,如下所示,用于存盤一個int值。
public class AP: DependencyObject
{
public static int Gettest ( DependencyObject obj )
{
return ( int ) obj . GetValue ( testProperty );
}
public static void Settest ( DependencyObject obj, int value )
{
obj . SetValue ( testProperty, value );
}
public static readonly DependencyProperty testProperty =
DependencyProperty . RegisterAttached ( "test", typeof ( int ), typeof ( AP ), new PropertyMetadata ( 2 ), Ontestchanged);
private static bool Ontestchanged ( object value )
{
Console . WriteLine ($"test value changed to {value}");
return true;
}
}
在我的 XAML 中,我設定了models:AP.test="21"按預期作業的值,由我WriteLine在 AP 中的除錯驗證。
但是,我想將此值作為 a 中的一列ListBox,因此具有DataTemplate具有以下條目的 a :
<TextBlock x:Name="tst"
Text="{Binding Path=(models:AP.test), RelativeSource={RelativeSource Self}}"
Width="55"/>
遺憾的是,我在我ListBox的21. 我已在我的ListBox屬性中將其設定為。我已經在 XAML 檔案中宣告了命名空間,如下所示。
xmlns:models="clr-namespace:WPFPages.ViewModels"
我已經閱讀了關于附加屬性的所有可能的文章,并在語法方面嘗試了主題的更多變體來完成這項作業,但根本沒有成功。到目前為止,我已經花了 3 天時間試圖讓這個簡單的 AP 作業。
由于我Binding對 .他們當然作業?
UPDATE 而不是使用 Comment :
<ListBox x:Name="listboxclass"
ItemContainerStyle="{StaticResource lvItemStyle1}"
BorderThickness="2"
BorderBrush="{StaticResource Blue0}"
FontWeight="Normal"
FontSize="14" Margin="0,0,0,164" >
<ListBoxItem
Height="45"
models:AP.test="21"/>
相關Style代碼是:
希望這能澄清它。我很高興現在選擇的列中出現相同的值。
<Style x:Key="lvItemStyle1" TargetType="{x:Type ListBoxItem}">
<!--<Setter Property="Background" Value="{Binding (models:AP.Background)}"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<!--<Border x:Name="Bd"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<Border.Background>
<SolidColorBrush x:Name="borderbckgrnd" Color="{TemplateBinding models:AP.Background}" />
</Border.Background>
<ContentPresenter x:Name="contentpresenter"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
</ContentPresenter>
</Border>-->
<!--I have tried ALL of these & they ALL cause Exceptions on loading
"{Binding models:AP.test}"
"{Binding Path=(models:AP.test)}"
"{Binding (models:AP.test)}"
"{Binding Source=models:AP.test}"
"{TemplateBinding models:AP.test}"-->
<Border x:Name="Bd" >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding CustomerId}"
Height="{Binding ActualHeight, ElementName=NwCustomerDataTemplateBorder1}"
Width="50"
Padding="1"
x:Name="CustomerId"/>
<!--NB Output window shows :
AP : test value changed to 2
AP : test value changed to 21
so it is definitely being set.
but the field is empty in the list when run-->
<TextBlock x:Name="tst"
Text="{Binding Path=models:Attach.test}"
Foreground="Red"
Width="55"/>
<!--this does not give error, but does not work either
Height="{Binding Path=models:AP.test, RelativeSource={RelativeSource Self}}"-->
<TextBlock Text="{Binding CompanyName}" Width="165" Padding="1" x:Name="CompanyName"/>
<TextBlock Text="{Binding ContactName}" Width="135" Padding="1" x:Name="ContactName"/>
<TextBlock Text="{Binding ContactTitle}" Width="40" Padding="1" x:Name="ContactTitle"/>
<TextBlock Text="{Binding Address}" Width="150" Padding="1" x:Name="Address"/>
<TextBlock Text="{Binding City }" Width="80" Padding="1" x:Name="City"/>
<TextBlock Text="{Binding PostalCode}" Width="75" Padding="1" x:Name="PostalCode"/>
<TextBlock Text="{Binding Country}" Width="75" Padding="1" x:Name="Country"/>
<TextBlock Text="{Binding Phone}" Width="95" Padding="1" x:Name="Phone"/>
<TextBlock Text="{Binding Fax}" Width="95" Padding="1" x:Name="Fax"/>
</StackPanel>
</Border>
<!-- ... -->
uj5u.com熱心網友回復:
這就是您可以系結到 中父項的附加屬性ListBox的方式ItemTemplate:
<ListBox models:AP.test="21">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="tst"
Text="{Binding Path=(models:AP.test),
RelativeSource={RelativeSource AncestorType=ListBox}}"
Width="55"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
uj5u.com熱心網友回復:
附加AP.test屬性在 上定義ListBoxItem,因此您需要參考該元素才能獲得分配的值,否則系結將不起作用。您的試驗不起作用,因為:
{Binding models:AP.test}- 這不是系結附加屬性的正確語法,請使用括號(和),請參閱系結路徑語法。{Binding Path=(models:AP.test)}- 正確的語法,但附加的屬性將在元素本身上決議,這里的TextBlock, 沒有定義它。{Binding (models:AP.test)}- 同上。{Binding Source=models:AP.test}- 將附加屬性設定為系結源,這是錯誤的。{TemplateBinding models:AP.test}- 不適用于附加屬性。
您可以通過使用附加的屬性系結語法并參考ListBoxItem定義值的來使系結作業。由于TextBlock是ListBoxItem模板的一部分,您可以使用RelativeSource與TemplateParent.
<TextBlock x:Name="tst"
Text="{Binding (local:AP.test), RelativeSource={RelativeSource TemplatedParent}}"
Foreground="Red"
Width="55" />
或者,您可以指定AncestorType,作為ListBoxItem的父級ListBoxItem。
<TextBlock x:Name="tst"
Text="{Binding (models:AP.test), RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
Foreground="Red"
Width="55" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313971.html
上一篇:使用XAML顯示類的所有屬性
