私はListViewを持つb_StringBoolTemplateというUserControlを持っています。 ListViewのアイテムは、ItemsというDependencyPropertyにバインドされています。 ListViewには、テキストボックス用とチェックボックス用の2つの列があります。ユーザーコントロールのためのXAMLコードは:コードビハインドで ユーザーコントロールのListView列の表示メンバーをカスタムオブジェクトにバインドするにはどうすればよいですか?
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="AutoCleaner.b_StringBoolTemplate"
x:Name="b_StringBoolTemplate1"
d:DesignWidth="310" d:DesignHeight="374">
<Grid>
<ListView ItemsSource="{Binding ElementName=b_StringBoolTemplate1, Path= Items}">
<ListView.View>
<GridView>
<GridViewColumn Header="{Binding ElementName=b_StringBoolTemplate1, Path= StringLabel}" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding ElementName=StringBoolInfo, Path=Content}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{Binding ElementName=b_StringBoolTemplate1, Path=BoolLabel}" Width="50" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding BoolValue, ElementName=b_StringBoolInfo}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="50" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Name="btnDeleteItem" Content="Delete"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>
私のListViewItemから派生しStringContentとBOOLVALUEと呼ばれる2つの依存性を、持つb_StringBoolInfoと呼ばれるオブジェクトを持っています。 TextContext.TextをStringContentにバインドし、Checkbox.IsCheckedをBoolValueにバインドして、UserControlにb_StringBoolInfo型の項目を動的に追加することができます。
私の方法が最善の方法であるかどうかもわかりません。それが重要な場合に備えてヘッダーとItemsSourceをDendency Propsにバインドするとうまく動作します。
申し訳ありません私は少し曖昧です。事前にお返事いただきありがとうございます。
編集:申し訳ありませんが、私はそれを明確にしませんでした。 ウィンドウにあるUserControlのインスタンスにb_StringBoolInfo型の項目を追加しました。 TextBox.TextとCheckbox.IsCheckedをb_StringBoolのプロパティにバインドしようとしていますが、その方法はわかりません。 ListViewItem由来私はb_StringBoolInfoというクラスを書いたコードで
:私はb_StringBoolTemplateと呼ばれるウィンドウにユーザーコントロール、ユーザーコントロールオブジェクト以下のXAMLを追加した
public class b_StringBoolInfo :ListViewItem
{
public string StringContent
{
get { return (string)GetValue(StringContentProperty); }
set { SetValue(StringContentProperty, value); }
}
// Using a DependencyProperty as the backing store for StringContent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty StringContentProperty =
DependencyProperty.Register("StringContent", typeof(string), typeof(b_StringBoolInfo),new PropertyMetadata("String"));
public bool BoolValue
{
get { return (bool)GetValue(BoolValueProperty); }
set { SetValue(BoolValueProperty, value); }
}
// Using a DependencyProperty as the backing store for BoolValue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BoolValueProperty =
DependencyProperty.Register("BoolValue", typeof(bool), typeof(b_StringBoolInfo), new PropertyMetadata(true));
}
、及びそのアイテム
<local:b_StringBoolTemplate Margin="64,67,159,145" StringLabel="Folder Location" BoolLabel="Search SubFolders?">
<local:b_StringBoolTemplate.Items>
<local:b_StringBoolInfo StringContent="String" BoolValue="True"/>
</local:b_StringBoolTemplate.Items>
</local:b_StringBoolTemplate>
は**ない**は何の作業を語りました。 –
申し訳ありません私はOPを編集したので、明確にしませんでした。テキストボックスのテキストをStringContentにバインドしようとしています。チェックボックスをBoolValueに設定していますが、どちらもb_StringBoolInfoというカスタムオブジェクトのプロパティです。 –
しかし、私がバインドしようとするものは何も動作しません。私も 'Text = {Binding StringContent}'を試しましたが、うまくいきません。 –