ユーザーを選択しなコンボボックスから、他の送依存性
-
21-09-2019 - |
質問
アダプタのご使用は、ユーザが選択可能な方の日付が表示されます。多くの 標準datetimeフォーマット文字列 選択できます。私は、平均ユーザーだが、違いを理解する"m"、"D"です。 いきたいと思いは このように、excelのはなのフォーマット文字列、どのように、任意の日のようになる形式です。
のコンポーネントのラインナップはコンボボックスSelectedItemは、依存関係を有の日付フォーマットのピッカーのセンチメータ級測位補強は、他の制御を含むこの日付ピッカーにも結合します。
例:
- ユーザ選択"は、15"の依存性は"m"。
- よりコードの後ろの値依存性は"D"では、コンボボックスを更新し表示"日(木)15月1970年"として選択します。
を使ってみました-コンバータがConvertBackったなと思抽出フォーマット文字列の作成に使用する。
解決
このクラス DateFormatChoice
を含む新しくオープンしたロフトタイプの形式コード(例えば、"m","D")および建てシェアハウスがオープンし、現在の日付の書式設定することができてうれしいです。
public class DateFormatChoice {
public string FormatCode { get; private set; }
public string CurrentDateExample {
get { return DateTime.Now.ToString( FormatCode ) }
}
public DateFormatChoice( string standardcode ) {
FormatCode = standardcode;
}
}
きを拘束するコンボボックスコレクション内のこれらの利用 CurrentDateExample
いずれにお DataTemplate
としてのコンボボックスの DisplayMemberPath
.どちらも使用可能でこれらのオブジェクトを直接自分の日付フォーマットにピッカークラスの DatePicker
に結合する FormatCode
物件の選択 DateFormatChoice
オブジェクトの設定ができます ValueMemberPath
性のコンボボックスの FormatCode
財産の利用 SelectedValue
のコンボボックスを取得/設定か選択します。を使用しない ValueMember
もう少し楽になります。
こちらはより全例です。使用する DateFormatChoice
クラスです。
まず、データを収集します。
public class DateFormatChoices : List<DateFormatChoice> {
public DateFormatChoices() {
this.Add( new DateFormatChoice( "m" ) );
this.Add( new DateFormatChoice( "d" ) );
this.Add( new DateFormatChoice( "D" ) );
}
}
そのシンプルでViewModelウィンドウのウィンドウ:
public class ViewModel : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged = ( s, e ) => {
}; // the lambda ensures PropertyChanged is never null
public DateFormatChoices Choices {
get;
private set;
}
DateFormatChoice _chosen;
public DateFormatChoice Chosen {
get {
return _chosen;
}
set {
_chosen = value;
Notify( PropertyChanged, () => Chosen );
}
}
public DateTime CurrentDateTime {
get {
return DateTime.Now;
}
}
public ViewModel() {
Choices = new DateFormatChoices();
}
// expression used to avoid string literals
private void Notify<T>( PropertyChangedEventHandler handler, Expression<Func<T>> expression ) {
var memberexpression = expression.Body as MemberExpression;
handler( this, new PropertyChangedEventArgs( memberexpression.Member.Name ) );
}
}
なかなかの日付ピッカーを制御し込みを受け付けた標準の文字列形式コードでしかダムUserControl(多角カット)だけが発揮されることにより、領収書の形式のコードです。えしました依存関係する DateFormatProperty
タイプ string
指定値の変更コールバックの UIPropertyMetadata
.
<Grid>
<TextBlock Name="datedisplayer" />
</Grid>
コールバック:
private static void DateFormatChanged( DependencyObject obj, DependencyPropertyChangedEventArgs e ) {
var uc = obj as UserControl1;
string code;
if ( null != ( code = e.NewValue as string ) ) {
uc.datedisplayer.Text = DateTime.Now.ToString( code );
}
}
こうしていつですべてのウインドウです。
<StackPanel>
<StackPanel.DataContext>
<local:ViewModel />
</StackPanel.DataContext>
<ComboBox
ItemsSource="{Binding Choices}" DisplayMemberPath="CurrentDateExample"
SelectedItem="{Binding Chosen, Mode=TwoWay}"/>
<local:UserControl1
DateFormatProperty="{Binding Chosen.FormatCode}" />
</StackPanel>