1

そのコントロールの ResourceDictionary 内から WPF コントロールの要素を参照しようとしています。次に例を示します。

<UserControl.Resources>
<ResourceDictionary>
    <Behaviors:GridViewInteractionModel x:Key="gridViewInteraction" GridView="{Binding ElementName=myGridView}"/>
</ResourceDictionary>
</UserControl.Resources>
...
<SomeGridView x:Name="myGridView"/>

GridViewオブジェクトの依存関係プロパティの値は、というGridViewInteractionModelオブジェクトである必要がありSomeGridViewますmyGridView

上記のコードは機能しません。 {Binding ElementName=myGridView}要素をバインドしません ( SetValue 関数GridViewInteractionModelが呼び出されることはありません)。

WPF ランタイム エラーは次のとおりです。

Cannot find source for binding with reference 'ElementName=myGridView'.
BindingExpression:(no path); DataItem=null; target element is 
'GridViewInteractionModel' (HashCode=15238415); target property is
'GridView' (type 'SomeGridView')

コントロール内の要素を ResourceDictionary 内のリソースのプロパティにバインドする方法を知っている人はいますか?

プロパティ セットを取得するために私が見つけた唯一の方法は、次のようにコード ビハインド コンストラクターで手動で設定することですInitializeComponent()

(Resources["gridViewInteraction"] as GridViewInteractionModel).GridView = FindName("myGridView") as SomeGridView;

しかし、それは本当に醜いです (そしてエラーが発生しやすくなります)。

どうも。

4

1 に答える 1

0

ここで何を達成しようとしているのか完全にはわかりません。これは、リストビューとその子アイテムをどのように処理したかの例です。

<Style x:Key="ListViewStyle" TargetType="ListView">
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFD3D1CF" Offset="0"/>
                <GradientStop Color="White" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect Opacity="0.7"/>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="ListViewItemStyle" TargetType="ListViewItem">
    <Setter Property="Background" Value="#FF494646"/>
    <Setter Property="Foreground" Value="#FFFDFAFA"/>
    <Setter Property="Height" Value="25"></Setter>
    <Setter Property="Cursor" Value="Hand"></Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
    </Style.Triggers>
</Style>


<ListView x:Name="lvwSettingsYears" x:Uid="lvwSettingsYears" Margin="10,77,10,50"
    Style="{DynamicResource ListViewStyle}"
    ItemContainerStyle="{DynamicResource ListViewItemStyle}"
    SelectionChanged="lvwYears_SelectionChanged">
    <ListView.View>
        <GridView>
            <GridViewColumn x:Name="SettingsYearListviewID" x:Uid="SettingsYearListviewID" DisplayMemberBinding="{Binding id}" Width="0"/>
            <GridViewColumn x:Name="SettingsYearListviewCode" x:Uid="SettingsYearListviewCode" Header="Code" DisplayMemberBinding="{Binding Code}" Width="100"/>
            <GridViewColumn x:Name="SettingsYearListviewDesc" x:Uid="SettingsYearListviewDesc" Header="Description" DisplayMemberBinding="{Binding Description}" Width="300"/>
        </GridView>
     </ListView.View>
 </ListView>

これを投稿するように促したのは、ターゲット プロパティが 'GridView' (type 'SomeGridView') であると言及しているエラー メッセージです。

コントロールタイプを探していると思います。

于 2013-03-07T20:43:31.643 に答える